延迟发布通知

发布于 2024-11-27 11:04:38 字数 4308 浏览 1 评论 0原文

我正在尝试通过 AlarmManager 延迟发布通知。为此,我创建了一个 AlarmReciever,当前如下所示:

public class NotificationAlarmReciever extends BroadcastReceiver{

public static final String TAG = "NotificationAlarmReciever";

public static final String KEY_NOTIFICATION_EXTRA = "NotificationExtra";
public static final String KEY_NOTIFICATION_ID = "NotificationId";


@Override
public void onReceive(Context context, Intent intent) {
    LogCat.d(TAG, "AlarmReciever Triggered");
    LogIntent(intent);
    Bundle b = intent.getExtras();
    if (b != null){
        if (b.containsKey(KEY_NOTIFICATION_EXTRA)){
            Notification n = b.<Notification>getParcelable(KEY_NOTIFICATION_EXTRA);
                    int request = 100
                    if(b.containsKey(KEY_NOTIFICATION_ID)){
                        request = b.getInt(KEY_NOTIFICATION_ID)
                    }
            NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(request, n);
        }
    }
}

private void LogIntent(Intent intent) {
    //logs Intent....

}
}

我创建了一个 TestActivty,在其中尝试启动一个 Alarm,如下所示:

public void onClick(View v) {
            Context c = getApplicationContext();
            int requestCode = 333;

            Notification notification = new Notification(R.drawable.balloon_overlay_close,"ticker", System.currentTimeMillis() + Integer.parseInt(mWhen.getText().toString()));
            Intent notificationAction = new Intent(c, TestActivity.class);
            PendingIntent notificationPendingOperation = PendingIntent.getActivity(c, requestCode, notificationAction, 0);
            notification.setLatestEventInfo(c, "HEADER", "MESSAGE", notificationPendingOperation);

            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent(c, NotificationAlarmReciever.class);
            alarmIntent.putExtra(NotificationAlarmReciever.KEY_NOTIFICATION_EXTRA, notification);
            PendingIntent alarmOperation = PendingIntent.getBroadcast(c, requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Line 127
            alarmManager.set(requestCode, notification.when, alarmOperation);
}

一旦调用 AlarmManager.set,我就会收到一个 NullPointerException 消息:

08-01 16:37:15.592: ERROR/AndroidRuntime(4422): FATAL EXCEPTION: main
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): java.lang.NullPointerException
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Parcel.readException(Parcel.java:1328)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Parcel.readException(Parcel.java:1276)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:174)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.AlarmManager.set(AlarmManager.java:139)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at de.eos.uptrade.android.core.TestActivity$1.onClick(TestActivity.java:127)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.view.View.performClick(View.java:2485)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.view.View$PerformClick.run(View.java:9080)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Handler.handleCallback(Handler.java:587)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Looper.loop(Looper.java:130)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at dalvik.system.NativeStart.main(Native Method)

我不知道,我做错了什么。

I'm trying to post Notifications via AlarmManager delayed. For this purpose I created an AlarmReciever which currently looks like this:

public class NotificationAlarmReciever extends BroadcastReceiver{

public static final String TAG = "NotificationAlarmReciever";

public static final String KEY_NOTIFICATION_EXTRA = "NotificationExtra";
public static final String KEY_NOTIFICATION_ID = "NotificationId";


@Override
public void onReceive(Context context, Intent intent) {
    LogCat.d(TAG, "AlarmReciever Triggered");
    LogIntent(intent);
    Bundle b = intent.getExtras();
    if (b != null){
        if (b.containsKey(KEY_NOTIFICATION_EXTRA)){
            Notification n = b.<Notification>getParcelable(KEY_NOTIFICATION_EXTRA);
                    int request = 100
                    if(b.containsKey(KEY_NOTIFICATION_ID)){
                        request = b.getInt(KEY_NOTIFICATION_ID)
                    }
            NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(request, n);
        }
    }
}

private void LogIntent(Intent intent) {
    //logs Intent....

}
}

I created a TestActivty where I try to start an Alarm like this:

public void onClick(View v) {
            Context c = getApplicationContext();
            int requestCode = 333;

            Notification notification = new Notification(R.drawable.balloon_overlay_close,"ticker", System.currentTimeMillis() + Integer.parseInt(mWhen.getText().toString()));
            Intent notificationAction = new Intent(c, TestActivity.class);
            PendingIntent notificationPendingOperation = PendingIntent.getActivity(c, requestCode, notificationAction, 0);
            notification.setLatestEventInfo(c, "HEADER", "MESSAGE", notificationPendingOperation);

            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent(c, NotificationAlarmReciever.class);
            alarmIntent.putExtra(NotificationAlarmReciever.KEY_NOTIFICATION_EXTRA, notification);
            PendingIntent alarmOperation = PendingIntent.getBroadcast(c, requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Line 127
            alarmManager.set(requestCode, notification.when, alarmOperation);
}

as soon as alarmManager.set is called I get a NullPointerException saying:

08-01 16:37:15.592: ERROR/AndroidRuntime(4422): FATAL EXCEPTION: main
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): java.lang.NullPointerException
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Parcel.readException(Parcel.java:1328)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Parcel.readException(Parcel.java:1276)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:174)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.AlarmManager.set(AlarmManager.java:139)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at de.eos.uptrade.android.core.TestActivity$1.onClick(TestActivity.java:127)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.view.View.performClick(View.java:2485)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.view.View$PerformClick.run(View.java:9080)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Handler.handleCallback(Handler.java:587)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.os.Looper.loop(Looper.java:130)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422):     at dalvik.system.NativeStart.main(Native Method)

I dont know, what I am doing wrong.

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

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

发布评论

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

评论(1

清引 2024-12-04 11:04:38

查看 AlarmManager.set(...) 文档

在那里你可以看到第一个参数'type'不能是333。它可以设置为ELAPSED_REALTIME、ELAPSED_REALTIME_WAKEUP、RTC或RTC_WAKEUP,它们对应于整数值3、2、1和0。这些是你的选择。

Take a look at AlarmManager.set(...) documentation.

There you can see that the first parameter 'type' cannot be 333. It can be set to either ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC or RTC_WAKEUP, which correspond to the integer values 3, 2, 1 and 0. Those are your choices.

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