Service Intent getaction and getextra return null

问题描述 投票:0回答:1

我有一个非常糟糕的问题我有一个通知服务,当我测试它工作正常时,它的工作正常,我关闭/打开了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,但是它也不起作用。

所以任何解决方案都是我做错了。

java android firebase android-studio service
1个回答
0
投票

如果不确定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();
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.