【发布时间】:2026-01-30 02:20:06
【问题描述】:
我有一个非常糟糕的问题。 我有一项工作正常的通知服务。当我测试它工作正常时,我关闭/打开 10 次以查看服务是否失败或返回 null,但它工作正常。我也多次关闭应用程序,看看它是否返回 null,但它工作正常。 但有时当我使用我的手机应用程序时显示强制关闭,例如当我使用其他应用程序时,firebase 的崩溃系统说 getaction 或 intent 为空。
我做错了什么?
这是我的服务代码:
@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 但它也不起作用。
【问题讨论】:
-
如果您不确定意图是否为空,但您仍在拨打电话 int getwhattodo = intent.getIntExtra("service_call",0); if (intent == null || intent.getAction() == null || getwhattodo == 0) { Log.e("receiver","service failed"); Crashlytics.log("服务调用失败"); if (getwhattodo == 0) { stopSelf(); } }
-
@ManojMohanty 当我测试应用程序时查看它是否返回 null 它永远不会返回 null 我测试 10 次它工作正常我可以在 android studio logcat 中看到但是当我每天使用我的手机时应用程序强制关闭所以当我在 firebase 中看到它说 null ref 时,你可以看到
-
坦率地说,我不确定为什么会发生这种情况,但是如果您看到代码错误返回 null 意图,并且您正尝试使用该 null 意图执行可能导致错误的进一步操作它只是您可以做的另一项检查
-
@ManojMohanty 感谢您的建议,但是当我不工作而不是在我测试它时(从不返回 null)
标签: java android firebase android-studio service