我在做一個應用程序,使當手機收到電話事情發生,所以很多搜索後,我發現我可以用一個BroadcastReceiver
和Service
所以我創建了一個Service
稱爲PhoneStateService
:Android - 爲什麼應用程序崩潰時服務收到來電並且應用程序已關閉?
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneStateService extends Service {
private static BroadcastReceiver broadcastReceiver;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
registerBroadcastReceiver();
}
@Override
public void onDestroy() {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
private void registerBroadcastReceiver() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
new PhoneStateHandler().onIncomingCallStarted(context);
}
}
};
IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(broadcastReceiver, filter);
}
}
這是用於開始PhoneStateService
(內側MainActivity
)的代碼:從所述Manifest
文件
if(PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PermissionChecker.PERMISSION_GRANTED) {
startService(new Intent(getApplicationContext(), PhoneStateService.class));
}
部分:
<service android:name=".Service.PhoneStateService" />
當手機接到電話並且應用程序正在運行(它仍然沒有銷燬)時,一切正常,但關閉應用程序後,手機接到應用程序崩潰後給我的消息應用已停止工作。
請告訴我是什麼問題,我沒有找到答案。
編輯:
例外:
09-11 10:26:30.598 29461-29461/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.multiplies.multiring, PID: 29461
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SCREEN_OFF flg=0x50000010 } in [email protected]
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.multiplies.multiring.Service.PhoneStateService$1.onReceive(PhoneStateService.java:36)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
編輯:
我做了什麼,有人說:
String stateStr = intent.getExtras() != null ? intent.getExtras().getString(TelephonyManager.EXTRA_STATE) : "";
把不能解決的問題。 我認爲它不會工作,因爲如果intent.getExtras()
是返回null,則if statement
不會因爲它返回null工作,它是想TelephonyManager.EXTRA_STATE_RINGING
:
if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
new PhoneStateHandler().onIncomingCallStarted(context);
}
,但感謝你的幫助。
HUGE EDIT:所述Manifest
的
部分:
<service android:name=".PhoneState.PhoneStateService" />
<receiver android:name=".PhoneState.PhoneStateBroadcast">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
PhoneStateBroadcast
:
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneStateBroadcast extends BroadcastReceiver {
Context context;
public void onReceive(Context context, Intent intent) {
this.context = context;
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
switch (telephonyManager.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
context.startService(new Intent(context, PhoneStateService.class));
break;
}
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
}
PhoneStateService
:
import android.app.IntentService;
import android.content.Intent;
public class PhoneStateService extends IntentService {
public PhoneStateService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
new PhoneStateHandler().onIncomingCallStarted(getApplicationContext());
}
}
此編輯後我仍然崩潰。
分享堆棧跟蹤 –
我編輯的問題 –
@AzzamAlsharafi看起來像u有NLE在intent.getExtras()的getString(TelephonyManager。 EXTRA_STATE)檢查。 ** intent.getExtras()**爲空 – Raghavendra