简体   繁体   English

服务意图 getaction 和 getextra 返回 null

[英]Service intent getaction and getextra return null

I have a really bad problem I have one notification service wich its work fine when i test it works just fine i turn off/on 10 times to see if service failed or return null but it works fine i also close application many time to see if its return null but its work fine.我有一个非常糟糕的问题我有一个通知服务,当我测试它时它工作正常我关闭/打开 10 次以查看服务是否失败或返回 null 但它工作正常我也多次关闭应用程序以查看是否它返回 null 但它工作正常。 but sometimes when I'm daily using my phone application show force close such as when I'm using other apps , crash system of firebase say getaction or intent is null.但有时当我每天使用我的手机应用程序显示强制关闭时,例如当我使用其他应用程序时,firebase 的崩溃系统说 getaction 或 intent 为空。 plz help me whats im doing wrong请帮助我我做错了什么

crash system崩溃系统在此处输入图片说明

在此处输入图片说明

this is my service code:这是我的服务代码:

 @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;

}

this is my receiver that call my service:这是我的接收器,呼叫我的服务:

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

i also tired: use start_stick service it wont work.我也累了:使用 start_stick 服务它不会工作。 im trying use intent extra int instead of getaction but it didn't work either.我尝试使用 Intent extra int 而不是 getaction 但它也不起作用。

so any solution for this whats I'm doing wrong.所以我做错了什么的任何解决方案。

if you are not sure if intent is null or not but still you are making a call just move int getwhattodo = intent.getIntExtra("service_call",0);如果您不确定意图是否为空,但您仍在拨打电话,只需移动 int getwhattodo = intent.getIntExtra("service_call",0); call inside your null check在您的空检查中调用

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM