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

发布于 2024-11-01 20:56:03 字数 5461 浏览 0 评论 0原文

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

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

@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 错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-11-08 20:56:03

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

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

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.

如若梦似彩虹 2024-11-08 20:56:03

您是否在注册 BroadcastReceiver 的同时将以下内容添加到 AndroidManifest.xml 中?

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文