来自 BroadcastReceiver 的呼叫通知

发布于 2024-10-31 15:30:48 字数 2471 浏览 4 评论 0原文

我有代码:

public void AlarmStart() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 5);
    Intent intent = new Intent(MainNote.this, AlarmReceiver.class);
    intent.putExtra("alarm_message", "MESS");
    PendingIntent sender = PendingIntent.getBroadcast(MainNote.this, 1,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

它按时调用 AlarmReceiver 类。

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String message = bundle.getString("alarm_message");
        NotifierHelper.sendNotification(?????, MainNote.class, "ba", "baba",
            2, true, true);
    } // Problem here
}

然后NotifierHelper类:

public class NotifierHelper {

    private static final int NOTIFY_1 = 0x1001;

    public static void sendNotification(Activity caller,
            Class<?> activityToLaunch, String title, String msg,
            int numberOfEvents, boolean flashLed, boolean vibrate) {
        NotificationManager notifier = (NotificationManager) caller
                .getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notify = new Notification(R.drawable.icon, "",
                System.currentTimeMillis());
        notify.icon = R.drawable.icon;
        notify.tickerText = "New Alerts";
        notify.when = System.currentTimeMillis();
        notify.number = numberOfEvents;
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        if (flashLed) {
            // add lights
            notify.flags |= Notification.FLAG_SHOW_LIGHTS;
            notify.ledARGB = Color.CYAN;
            notify.ledOnMS = 500;
            notify.ledOffMS = 500;
        }
        if (vibrate) {
            notify.vibrate = new long[] { 100, 200, 200, 200, 200, 200, 1000,
                    200, 200, 200, 1000, 200 };
        }
        Intent toLaunch = new Intent(caller, activityToLaunch);
        PendingIntent intentBack = PendingIntent.getActivity(caller, 0,
            toLaunch, 0);
        notify.setLatestEventInfo(caller, title, msg, intentBack);
        notifier.notify(NOTIFY_1, notify);
    }
}

如何从AlarmReceiver传递Activity caller

I have the code :

public void AlarmStart() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 5);
    Intent intent = new Intent(MainNote.this, AlarmReceiver.class);
    intent.putExtra("alarm_message", "MESS");
    PendingIntent sender = PendingIntent.getBroadcast(MainNote.this, 1,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

It calls AlarmReceiver class on time.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String message = bundle.getString("alarm_message");
        NotifierHelper.sendNotification(?????, MainNote.class, "ba", "baba",
            2, true, true);
    } // Problem here
}

Then NotifierHelper class:

public class NotifierHelper {

    private static final int NOTIFY_1 = 0x1001;

    public static void sendNotification(Activity caller,
            Class<?> activityToLaunch, String title, String msg,
            int numberOfEvents, boolean flashLed, boolean vibrate) {
        NotificationManager notifier = (NotificationManager) caller
                .getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notify = new Notification(R.drawable.icon, "",
                System.currentTimeMillis());
        notify.icon = R.drawable.icon;
        notify.tickerText = "New Alerts";
        notify.when = System.currentTimeMillis();
        notify.number = numberOfEvents;
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        if (flashLed) {
            // add lights
            notify.flags |= Notification.FLAG_SHOW_LIGHTS;
            notify.ledARGB = Color.CYAN;
            notify.ledOnMS = 500;
            notify.ledOffMS = 500;
        }
        if (vibrate) {
            notify.vibrate = new long[] { 100, 200, 200, 200, 200, 200, 1000,
                    200, 200, 200, 1000, 200 };
        }
        Intent toLaunch = new Intent(caller, activityToLaunch);
        PendingIntent intentBack = PendingIntent.getActivity(caller, 0,
            toLaunch, 0);
        notify.setLatestEventInfo(caller, title, msg, intentBack);
        notifier.notify(NOTIFY_1, notify);
    }
}

How can I pass Activity caller from AlarmReceiver?

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

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

发布评论

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

评论(1

他夏了夏天 2024-11-07 15:30:48

我认为您不需要对 NotifierHelper 中的 Activity 进行任何引用。使用Context(Activity是其子类),例如:

public static void sendNotification(Context caller, ...

getSystemService()等方法实际上是由Context公开的。

由于您在 AlarmReceiver.onReceive() 中传递了一个 Context,因此您可以继续传递它。

I think that you don't need any reference to an Activity in the NotifierHelper. Use the Context (which Activity is a subclass of), eg:

public static void sendNotification(Context caller, ...

Such methods as getSystemService(), etc.. are actually exposed by Context.

And since you get passed a Context in AlarmReceiver.onReceive(), you can pass it on.

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