gpt4 book ai didi

java - 服务 Intent getaction 和 getextra 返回 null

转载 作者:行者123 更新时间:2023-12-01 17:50:21 25 4
gpt4 key购买 nike

我有一个非常糟糕的问题。我有一项工作正常的通知服务。当我测试它工作正常时,我关闭/打开 10 次以查看服务是否失败或返回 null,但它工作正常。我还多次关闭应用程序以查看其是否返回 null,但它工作正常。但有时当我使用手机应用程序时显示强制关闭,例如当我使用其他应用程序时,Firebase 崩溃系统说 getaction 或 Intent 为 null。

我做错了什么?

崩溃系统 enter image description here

enter image description here

这是我的服务代码:

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
sp = getSharedPreferences("accdata", Context.MODE_PRIVATE);
namesaveget = sp.getString("nameacc","No Account");
int getwhattodo = intent.getIntExtra("service_call",0);
if (intent == null || intent.getAction() == null || getwhattodo == 0) {
Log.e("receiver","service faild");
Crashlytics.log("service faild to call");
if (getwhattodo == 0) {
stopSelf();
}

}
if (intent != null && getwhattodo == 2) {
Log.e("receiver","call done");
createNotificationChannel();
showMainNotification();
showSubNotifications();
Crashlytics.log("service call noti");
}


return START_STICKY;

}

这是我的接收器,调用我的服务:

 @Override
public void onReceive(Context context, Intent intent) {
Log.e("receiver","work");

if (isNetworkConnected(context) == true) {


Log.e("receiver","wifi on");
Intent i = new Intent(context,ServiceNotifications.class);
i.putExtra("service_call",2);
context.startService(i);




} else {
Log.e("receiver","wifi off");
}



}

public static boolean isNetworkConnected(Context c) {
ConnectivityManager connectivityManager =
(ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

我也尝试过:使用 start_stick 服务将不起作用。我正在尝试使用 Intent extra int 而不是 getaction,但它也不起作用。

最佳答案

如果您不确定 Intent 是否为 null,但您仍在调用电话,只需移动 int getwhattodo = intent.getIntExtra("service_call",0);在空检查中调用

int getwhattodo = intent.getIntExtra("service_call",0);
if (intent == null || intent.getAction() == null || getwhattodo == 0) {
Log.e("receiver","service faild");
Crashlytics.log("service faild to call");
if (getwhattodo == 0) {
stopSelf();
}

}

关于java - 服务 Intent getaction 和 getextra 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60808645/

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