【问题标题】:Broadcast receiver, check a checkbox preference state on bootup then send a notification广播接收器,在启动时检查复选框首选项状态,然后发送通知
【发布时间】:2011-06-07 14:02:26
【问题描述】:

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

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

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

【问题讨论】:

  • 欢迎来到 SO。为了将来参考,许多用户使用特定语言,因此如果您标记该语言,您将更有可能收到答案。 :)
  • 在 Eclipse 中使用 adb logcat、DDMS 或 DDMS 透视图检查 LogCat 并查看与“强制关闭”相关的堆栈跟踪。
  • 出于调试原因忽略我的 toast 通知。

标签: java android broadcastreceiver android-notifications


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助。我认为问题还在于我将接收器放在项目的单独文件夹中,而这不在我的清单文件中。
【解决方案2】:

您是否将以下内容添加到您的 AndroidManifest.xml 以及注册BroadcastReceiver??

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多