如何获取拨出和拨入电话的电话号码信息

时间:2015-06-01 04:41:35

标签: android

我制作了一个广播接收器,每当发出拨出呼叫事件时,它都会取出拨出号码。现在我想将它整合到传入号码中。我正在分享代码。

callreceiver class

public class CallReceiver extends BroadcastReceiver {
    TelephonyManager tManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        final String OutgoingCallNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.i("CLApp", OutgoingCallNumber);
    }
}

清单文件

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

错误 - 记录:

06-01 10:57:36.209: E/dalvikvm(29762): Could not find class 'android.content.RestrictionsManager', referenced from method com.salesforce.androidsdk.config.RuntimeConfig.getRestrictions
06-01 10:57:36.219: E/dalvikvm(29762): Could not find class 'android.content.RestrictionsManager', referenced from method com.salesforce.androidsdk.config.RuntimeConfig.hasRestrictionsProvider
06-01 10:57:36.329: E/IMGSRV(29762): :0: PVRDRMOpen: TP3, ret = 49
06-01 10:57:36.209: E/dalvikvm(29762): Could not find class 'android.content.RestrictionsManager', referenced from method com.salesforce.androidsdk.config.RuntimeConfig.getRestrictions
06-01 10:57:36.219: E/dalvikvm(29762): Could not find class 'android.content.RestrictionsManager', referenced from method com.salesforce.androidsdk.config.RuntimeConfig.hasRestrictionsProvider
 06-01 10:57:36.329: E/IMGSRV(29762): :0: PVRDRMOpen: TP3, ret = 49
 06-01 10:57:36.329: E/IMGSRV(29762): :0: PVRDRMOpen: TP3, ret = 50
 06-01 10:57:36.329: E/IMGSRV(29762): :0: PVRDRMOpen: TP3, ret = 50
 06-01 10:57:36.329: E/IMGSRV(29762): :0: PVRDRMOpen: TP3, ret = 50
 06-01 10:57:52.709: E/AndroidRuntime(29762): FATAL EXCEPTION: main
 06-01 10:57:52.709: E/AndroidRuntime(29762): Process: io.concret.callloggerapp, PID: 29762
 06-01 10:57:52.709: E/AndroidRuntime(29762): java.lang.RuntimeException: Unable to start receiver io.concret.callloggerapp.CallReceiver: java.lang.NullPointerException: println needs a message
 06-01 10:57:52.709: E/AndroidRuntime(29762):   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2593)
 06-01 10:57:52.709: E/AndroidRuntime(29762):   at android.app.ActivityThread.access$1700(ActivityThread.java:139)
 06-01 10:57:52.709: E/AndroidRuntime(29762):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.os.Looper.loop(Looper.java:149)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.app.ActivityThread.main(ActivityThread.java:5257)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at java.lang.reflect.Method.invokeNative(Native Method)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at java.lang.reflect.Method.invoke(Method.java:515)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at dalvik.system.NativeStart.main(Native Method)
06-01 10:57:52.709: E/AndroidRuntime(29762): Caused by: java.lang.NullPointerException: println needs a message
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.util.Log.println_native(Native Method)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.util.Log.println_filter(Log.java:105)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.util.Log.i(Log.java:187)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at io.concret.callloggerapp.CallReceiver.onReceive(CallReceiver.java:23)
06-01 10:57:52.709: E/AndroidRuntime(29762):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
06-01 10:57:52.709: E/AndroidRuntime(29762):    ... 10 more

2 个答案:

答案 0 :(得分:0)

检查收到的意图是否适用于Receiver类中的来电或拨出电话

public class CallReceiver extends BroadcastReceiver {
TelephonyManager tManager;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
      // Outgoing call
      final String outgoingCallNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
      Log.i("CLApp", OutgoingCallNumber);
    }
    else {
         // Incoming call
         String incomingCallNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
         Log.i("CLApp", incomingCallnumber);
    }

}

答案 1 :(得分:0)

您可以从通话记录本身获取来电和去电通知信息。 下面的代码段会为您提供日期范围内的通话记录

final String SELECT = CallLog.Calls.DATE + ">?" + " AND "
      + CallLog.Calls.DATE + "<?";
Cursor managedCursor = context.getContentResolver().query(
      CallLog.Calls.CONTENT_URI,
      new String[] { CallLog.Calls.DATE, CallLog.Calls.DURATION,
            CallLog.Calls.TYPE, CallLog.Calls.NUMBER,
            CallLog.Calls._ID },
      SELECT,
      new String[] { String.valueOf(afterdate.getTime()),
            String.valueOf(beforeDate.getTime()) },
      CallLog.Calls.DATE + " desc");

int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

while (managedCursor.moveToNext()) {
   phNumber = managedCursor.getString(number);
   String callType = managedCursor.getString(type);
   String callDate = managedCursor.getString(date);
   Date callDayTime = new Date(Long.valueOf(callDate));
   final String callDuration = managedCursor.getString(duration);
   String dir = null;
   int callTypeCode = Integer.parseInt(callType);
   final SmsDetailsPojo dataItem = new SmsDetailsPojo();
   String contactName = Util.getContactName(context, phNumber);

   dataItem.setContactNum(phNumber);
   dataItem.setContactName(contactName);
   dataItem.setSmsCount(callDuration);


   arrContactDetails.add(dataItem);
}



e