【问题标题】:Want to save the outgoing call number, duration using broadcastreceiver要保存拨出电话号码,使用广播接收器的持续时间
【发布时间】:2014-05-04 02:23:54
【问题描述】:

我想在android中使用broadcastreceiver服务保存拨出电话号码和持续时间。我使用下面的代码来实现该功能,但它会引发错误。

public class OutgoingReceiver extends BroadcastReceiver {
    public OutgoingReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        try
        {
            Bundle bundle = intent.getExtras();
            number = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
            dbOutgoing = new DBOutgoing(ctx);            
            dbOutgoing.InsertOutGoingCallDB(number, "0", "0");
            Toast.makeText(ctx, 
                "Outgoing: "+number, 
                Toast.LENGTH_LONG).show();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            Toast.makeText(ctx, String.valueOf(e),Toast.LENGTH_LONG).show();
        }  
    }
}

上面的代码给出了拨出电话号码,但我还需要通话结束后的持续时间。

【问题讨论】:

  • 请添加错误详情。
  • @AlexeyMalev。我现在没有收到任何错误,但我想在拨出电话结束后获取通话时长

标签: java android


【解决方案1】:

发件人:Get Last Call Duration in androidIntent to be fired when a call ends?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gopi"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".IncomingCallTracker">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
</manifest> 



public class IncomingCallTracker extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

            Set<String> keys = bundle.keySet();
        for (String key : keys) {
                Log.i("MYAPP##", key + "="+ bundle.getString(key));
        }       
    }

}

您可以在包中查找键“状态”。当它的值为 'IDLE' 时,表示通话已结束,您可以基于此执行任何您想要的操作。

如果状态是“空闲”

 Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = mContext.getContentResolver().query(
                contacts, null, null, null, null);
        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
        int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        // movetoFirst() gives last ended call
        if( managedCursor.moveToFirst() == true ) {
            String phNumber = managedCursor.getString( number );
            String callDuration = managedCursor.getString( duration1 );
        }
        managedCursor.close();

【讨论】:

  • 感谢您的代码,但我如何才能找到捆绑包的状态。我试过 bundle.getString(TelephonyManager.EXTRA_STATE) 但我不知道它是否正确?
  • 我需要它来拨出电话。任何人都可以分享您的想法。
  • 用于拨出电话的广播接收器:stackoverflow.com/questions/9569118/…
  • 如果你执行上面的代码,你会看到可用的键列表。这里:Log.i("MYAPP##", key + "="+ bundle.getString(key));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多