-1

I have written a subclass of WakefulBroadcastReceiver that receives the "android.intent.action.BOOT_COMPLETED" action when user device is rebooted.

It is working fine in my Nexus device but not on some other devices like Samsung, Verizon etc. For those devices, I am getting a NullPointerException in onReceive() at this line Intent service = new Intent(context, MyAlarmOnBootService.class); as

java.lang.RuntimeException: Unable to start receiver com.android.myapp.receiver.MyWakefulAlarmOnBootReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2244)
at android.app.ActivityThread.access$1500(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4854)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.os.Bundle.putAll(Bundle.java:303)
at android.content.Intent.putExtras(Intent.java:5762)
at com.android.myapp.receiver.MyWakefulAlarmOnBootReceiver.onReceive(MyWakefulAlarmOnBootReceiver.java:30)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2237)
... 10 more 



This is my receiver class:

public class MyWakefulAlarmOnBootReceiver extends
        WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("MyApp", "receiver started after reboot");                

        Intent service = new Intent(context, MyAlarmOnBootService.class);//this is the line where I am getting NPE.
        service.putExtras(intent.getExtras());

        startWakefulService(context, service);
    }

}



My Manifest file (only the parts related are shown in this file):

<?xml version="1.0" encoding="utf-8"?>
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="25" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <receiver android:name=".receiver.MyWakefulAlarmOnBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    <service
        android:name=".service.MyAlarmOnBootService"
        android:enabled="true" />
...


Is this related to Smart Manager app of Samsung that kills apps and alarms? If yes, how is my receiver fired? Is there some problem in the context parameter in onReceive()? Please help me. It is working fine on my Nexus 5.

shanti
  • 359
  • 5
  • 20

1 Answers1

0

Intent service = new Intent(context, MyAlarmOnBootService.class);//this is the line where I am getting NPE.

actually this is not the line. the exception raised from next line

service.putExtras(intent.getExtras());

you created new intent which has no extras, and you pass it as a parameter to putExtra() this is where you are wrong.

Farid
  • 1,024
  • 9
  • 16
  • You are right to mention that exception is coming at service.putExtras(intent.getExtras()); So you mean to say that the parameter "intent" of onReceive() is null or its extras are null. But why is this device specific as Nexus 5 has it working? – shanti Mar 08 '17 at 09:57
  • the intent isn't null but the extra is,and i dont have access to nexus 5 so i dont know what is in its extras,if you debug it you can see what extras it has set – Farid Mar 08 '17 at 10:06
  • No I am saying that whatever extras I save are available when i run my app on Nexus 5 but not when it is Samsung or Verizon device. – shanti Mar 08 '17 at 10:08
  • 1
    @shanti What kind of information you need from BOOT_COMPLETED intent? The problem might be that diffrent devices might sent extra information with boot comleted and might not. My advice for you is to remove line where you get extras from intent as I don't see why would you need it – Vygintas B Mar 08 '17 at 10:53
  • @shanti or if you need it check if it has extras before – Vygintas B Mar 08 '17 at 10:54
  • @VygintasB Thanks a ton. This has solved it. I earlier thought this extras was necessary to run my notification in service. But commenting out that line and running in device that has this intent just works fine. If you send it as an answer, I will accept it as answer. – shanti Mar 08 '17 at 11:29
  • @shanti Accept this one. It answers your problem. My comment just explains about intent. – Vygintas B Mar 08 '17 at 12:09