本文介绍了来电通知从BroadcastReceiver的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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);
}

它调用AlarmReceiver按时上课。

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
}

然后NotifierHelper类:

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

如何传递活动来电从AlarmReceiver?

How can I pass Activity caller from AlarmReceiver?

推荐答案

我觉得你不需要在NotifierHelper凡提述的活动。使用上下文(其活性的一个子类),例如:

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, ...

这样的方法作为getSystemService()等。实际上是由语境露出。

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

既然你获得通过上下文中AlarmReceiver.onReceive(),你可以通过它。

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

这篇关于来电通知从BroadcastReceiver的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:04