Broadcast receiver, check a checkbox preference st

2020-05-04 19:26发布

问题:

My problem is that when I try to read a checkbox preference state from a different activity on bootup then send a status bar notification. Then when the device boots the I get a force close error message popup then when I go into the error log I don't understand what happens.

The code for the broadcast receiver is shown below:

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        //this creates a reference to my preferences activity   
        Prefs prefsC = new Prefs();

        SharedPreferences prefs = context.getSharedPreferences("Prefs", 0);

        int status = Integer.parseInt(prefs.getString("bootup", "-1"));
        if(status > 0){
            //notifyNS is a method that sends the status bar notification
            prefsC.notifyNS("", R.drawable.n);
            //the setCheckedNS method is just a custom method I made to set the state of a checkbox preference
            prefsC.setCheckedNS("icon", false);
        }else{
            prefsC.setCheckedNS("enable", false);
            prefsC.setCheckedNS("icon", false);
            prefsC.setCheckedNS("bootup", false);
        }
    }

}

So could you help me solve the issue on why it force closes on bootup. So basically what I want to do is read a checkbox preference state on bootup then send a status bar notification.

This is my error log response:

04-16 11:23:15.546: ERROR/AndroidRuntime(977): FATAL EXCEPTION: main
04-16 11:23:15.546: ERROR/AndroidRuntime(977): java.lang.RuntimeException: Unable to instantiate receiver com.brandon.labs.nsettings.receivers.notifyBootup: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2913)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.app.ActivityThread.access$3200(ActivityThread.java:135)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2198)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.os.Looper.loop(Looper.java:144)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at java.lang.reflect.Method.invoke(Method.java:521)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at dalvik.system.NativeStart.main(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): Caused by: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2904)
04-16 11:23:15.546: ERROR/AndroidRuntime(977):     ... 10 more

So I have no clue what to do from here.

Alright I have figured out what I have done wrong. What it is how I was connecting the context to the notificationManger construct method and Intent construct method.

Here is my new and revised code that works: `public class BootupReceiver extends BroadcastReceiver {

private static final boolean BOOTUP_TRUE = true;
private static final String BOOTUP_KEY = "bootup";

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

    if(getBootup(context)) {
        Toast toast2 = Toast.makeText(context, "getBootup", Toast.LENGTH_SHORT);
        toast2.show();

        NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification Notify = new Notification(R.drawable.n,
                "NSettings Enabled", System.currentTimeMillis());

        Notify.flags |= Notification.FLAG_NO_CLEAR;
        Notify.flags |= Notification.FLAG_ONGOING_EVENT;

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        Notify.contentView = contentView;

        Intent notificationIntent = new Intent(context, Toggles.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notify.contentIntent = contentIntent;

        Toast toast = Toast.makeText(context, "Notify about to be sent", Toast.LENGTH_SHORT);
        toast.show();

                    int HELO_ID = 00000;

        NotifyM.notify(HELLO_ID, Notify);
        Toast toast1 = Toast.makeText(context, "Notify sent", Toast.LENGTH_SHORT);
        toast1.show();
    }

    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.brandon.labs.nsettings.NotifyService");
    context.startService(serviceIntent);
}

public static boolean getBootup(Context context){
    return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE);
}

} `

Since this question has gotten more than 100 views I thought it would be nice of me to post the code that works properly.

Note: I don't know why the closing curly bracket for the class isn't showing with the rest of the code it's a stackoverflow error

回答1:

According to log ('java.lang.RuntimeException: Unable to instantiate receiver'), system is unable to create instance of your receiver class, because system is unable to find specified class (com.brandon.labs.nsettings.receivers.notifyBootup). I think it is probably problems with name (of receiver class) in your AndroidManifest.xml file, and it is not related to preferences.

In future, if you will get another exception, i recommend you to read carefully message of exception ;) and stack trace. Usually they contains the half of answer to your question. And for popular mistakes - you can try to type exception text into Google (excluding some specific info such as name of class), and you will find solution very fast.



回答2:

Have you added the following to your AndroidManifest.xml along with registering the BroadcastReceiver??

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />