私のアプリでは、 TelephonyManager の助けを借りてサービスを開始する意図を送信する BroadcastReceiver を使用しています...
@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)