Note that there are some explanatory texts on larger screens.

plurals
  1. POBroadcast receiver, check a checkbox preference state on bootup then send a notification
    primarykey
    data
    text
    <p>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.</p> <p>The code for the broadcast receiver is shown below:</p> <pre><code>@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 &gt; 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); } } } </code></pre> <p>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.</p> <p>This is my error log response:</p> <pre><code>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 </code></pre> <p>So I have no clue what to do from here.</p> <p>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.</p> <p>Here is my new and revised code that works: `public class BootupReceiver extends BroadcastReceiver {</p> <pre><code>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); } </code></pre> <p>} `</p> <p>Since this question has gotten more than 100 views I thought it would be nice of me to post the code that works properly.</p> <p>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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload