广播接收器,在启动时检查复选框首选项状态,然后发送通知

|| 我的问题是,当我尝试从启动时的其他活动中读取复选框首选项状态时,然后发送状态栏通知。然后,当设备启动时,我会收到一个强制关闭错误消息弹出窗口,然后当我进入错误日志时,我不知道会发生什么。 广播接收器的代码如下所示:
@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);
        }
    }

}
因此,您能帮我解决为什么它在启动时强制关闭的问题吗?因此,基本上我想做的是在启动时读取复选框首选项状态,然后发送状态栏通知。 这是我的错误日志响应:
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
所以我不知道从这里开始该怎么做。 好吧,我已经弄清楚了我做错了什么。我将上下文连接到notificationManger构造方法和Intent构造方法的方式是什么。 这是我新的和修改后的代码,可以正常工作: 公共类BootupReceiver扩展了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);
}
} ` 既然这个问题已经获得了100多个意见,我认为发布能正常工作的代码对我来说是一件很高兴的事情。 注意:我不知道为什么该类的结尾大括号没有与其余代码一起显示,这是一个stackoverflow错误     
已邀请:
根据日志(\'java.lang.RuntimeException:无法实例化接收器\'),系统无法创建接收器类的实例,因为系统无法找到指定的类(com.brandon.labs.nsettings.receivers。 notifyBootup)。我认为您的AndroidManifest.xml文件中的(接收方类的)名称可能有问题,并且与首选项无关。 将来,如果您还会收到另一个异常,我建议您仔细阅读异常;)和堆栈跟踪的消息。通常它们包含您问题的一半答案。 对于常见的错误-您可以尝试在Google中键入异常文本(不包括某些特定信息,例如类名),您会很快找到解决方案。     
您是否已将以下内容添加到AndroidManifest.xml中,并注册了
BroadcastReceiver
<uses-permission android:name=\"android.permission.RECEIVE_BOOT_COMPLETED\" />
    

要回复问题请先登录注册