gpt4 book ai didi

java - Android - 为什么当服务收到调用并且应用程序关闭时应用程序崩溃?

转载 作者:太空狗 更新时间:2023-10-29 16:27:38 24 4
gpt4 key购买 nike

我正在制作一个应用程序,当电话接到电话时会发生一些事情,所以经过大量搜索后我发现我可以使用 BroadcastReceiverService所以我创建了一个名为 PhoneStateServiceService:

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 中):

if(PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PermissionChecker.PERMISSION_GRANTED) {
startService(new Intent(getApplicationContext(), PhoneStateService.class));
}

来自 Manifest 文件的部分:

<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 com.multiplies.multiring.Service.PhoneStateService$1@1924f18
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);
}

但是谢谢你的帮助。

巨大的编辑:

list 的一部分:

<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);
}

}
}

电话状态服务:

    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());
}
}

在这次修改后我仍然崩溃。

最佳答案

出于某种原因,额外的是空的。您正在执行 intent.getExtras().XXXintent.getExtras() 返回 null,对其调用 .XXX 会引发错误。

改变

String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);

进入

String stateStr = intent.getExtras() != null ? intent.getExtras().getString(TelephonyManager.EXTRA_STATE) : "";

将阻止崩溃的发生,但您可能想调查一下为什么您期望额外的但没有任何 Intent 的原因。

关于java - Android - 为什么当服务收到调用并且应用程序关闭时应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com