Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 广播接收器,在启动时选中复选框首选项状态,然后发送通知_Java_Android_Broadcastreceiver_Android Notifications - Fatal编程技术网

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

Java 广播接收器,在启动时选中复选框首选项状态,然后发送通知,java,android,broadcastreceiver,android-notifications,Java,Android,Broadcastreceiver,Android Notifications,我的问题是,当我尝试在启动时从其他活动读取复选框首选项状态时,会发送状态栏通知。然后,当设备启动时,我会弹出一条强制关闭错误消息,当我进入错误日志时,我不知道发生了什么 广播接收器的代码如下所示: @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ //this creat

我的问题是,当我尝试在启动时从其他活动读取复选框首选项状态时,会发送状态栏通知。然后,当设备启动时,我会弹出一条强制关闭错误消息,当我进入错误日志时,我不知道发生了什么

广播接收器的代码如下所示:

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

}
那么,你能帮我解决为什么it强制在启动时关闭的问题吗。所以基本上我想做的是在启动时读取一个复选框首选项状态,然后发送一个状态栏通知

这是我的错误日志响应:

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多个视图,所以我想如果我能发布能够正常工作的代码就太好了

注意:我不知道为什么该类的结束方括号没有与其余代码一起显示它是一个堆栈溢出错误。

根据日志('java.lang.RuntimeException:无法实例化receiver'),系统无法创建receiver类的实例,因为系统无法找到指定的类(com.brandon.labs.nsettings.receivers.notifyBootup)。我认为可能是AndroidManifest.xml文件中的(接收方类的)名称有问题,并且与首选项无关

将来,若您将遇到另一个异常,我建议您仔细阅读异常消息;)和堆栈跟踪。通常它们包含你问题的一半答案。
对于常见错误-您可以尝试在Google中键入异常文本(不包括某些特定信息,如类名),您会很快找到解决方案。

您是否在注册
广播接收器的同时将以下内容添加到AndroidManifest.xml中


欢迎来到SO。为便于将来参考,许多用户使用特定的语言,因此,如果您标记该语言,您将更有可能收到答案。:)使用Eclipse中的adb logcat、DDMS或DDMS透视图检查logcat并查看与“强制关闭”相关联的堆栈跟踪。忽略我的toast通知,它们是出于调试原因。感谢帮助。我认为问题还在于,我的项目中有一个单独的文件夹,而该文件夹不在我的清单文件中。