Android TelephonyManager检测未接听电话

时间:2012-07-05 10:45:17

标签: android android-intent

在我的应用程序中,我使用BroadcastReceiver,它在TelephonyManager的帮助下发送意图启动服务......

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

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) 
    {
        IntentService = new Intent(context, PlayService.class).setAction("outgoing_call");
        IntentService.putExtra("phone_number",intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));  
        context.startService(IntentService);            
    }
    else
    {
        tl= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
        int type = tl.getCallState();

        switch (type) {

        case TelephonyManager.CALL_STATE_OFFHOOK:



            break;

        case TelephonyManager.CALL_STATE_RINGING:
            Toast.makeText( context, "incoming call", Toast.LENGTH_LONG).show();
            IntentService = new Intent(context, PlayService.class).setAction("incoming_call");
            IntentService.putExtra("phone_number",intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) );
            if (SmsReceiver.bool)
            context.startService(IntentService);
            break;

        case TelephonyManager.CALL_STATE_IDLE:
            break;
        }

    }
}  

当手机响铃且没有应答时,应用程序崩溃并出现此类错误...如何检测此类情况

07-05 14:39:37.304: E/AndroidRuntime(11357): java.lang.RuntimeException: Unable to stop service com.samples.broadcastreceiver.PlayService@405364b8: java.lang.NullPointerException
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.app.ActivityThread.handleStopService(ActivityThread.java:2086)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.app.ActivityThread.access$2900(ActivityThread.java:117)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.os.Looper.loop(Looper.java:130)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at dalvik.system.NativeStart.main(Native Method)
07-05 14:39:37.304: E/AndroidRuntime(11357): Caused by: java.lang.NullPointerException
07-05 14:39:37.304: E/AndroidRuntime(11357):    at com.samples.broadcastreceiver.PlayService.onDestroy(PlayService.java:77)
07-05 14:39:37.304: E/AndroidRuntime(11357):    at android.app.ActivityThread.handleStopService(ActivityThread.java:2069)

0 个答案:

没有答案