称为AlarmManager通知活动不弹出,当应用程序被关闭 [英] Notification activity called by AlarmManager NOT to pop up when app is closed

查看:136
本文介绍了称为AlarmManager通知活动不弹出,当应用程序被关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关自定义提醒应用程序,我使用 AlarmManager PendingIntent 来设置具体的时间我通知弹出。

For a custom reminder app, I'm using AlarmManager and PendingIntent to set a specific time for my Notification to pop up.

我有我的 NotificationManager ReceiverActivity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_receiver);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)
            getSystemService(ns);
    Context context = getApplicationContext();
    CharSequence ticker = "You have notification";
    CharSequence contentTitle = "My Reminder";
    CharSequence contentText = "Reminder Content";
    Notification notification = new Notification(R.drawable.notif_icon,
            ticker, System.currentTimeMillis());
    Intent notificationIntent = new Intent(this,
            AlarmReceiverActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText,
        contentIntent);
    final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
}

在ReceiverActivity弹出在指定的时间加上在通知栏(左上)的通知,一切都很好。但它不是那么人性化,我只想通知出现在没有任何活动的通知栏弹出,当应用程序被关闭(如在现实的情况下,可能是当时的提醒是由于关闭)

The ReceiverActivity pops up at the specified time plus the notification in the notification bar (upper left) and all is well. But its not so user friendly, I want only the notification to appear in the notification bar without any activities popping up when app is closed (where in realistic situations, is probably closed at the time your reminder is due)

推荐答案

定义你的 AlarmManager PendingIntent 来被接收的的BroadcastReceiver

Define your AlarmManager's PendingIntent to be received by a BroadcastReceiver

Intent i = new Intent(context, YourReceiver.class);
PendingIntent alarmPendingIntent= PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_UPDATE_CURRENT);

然后

public class YourReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // use a BUILDER all this is deprecated
        CharSequence ticker = "You have notification";
        CharSequence contentTitle = "My Reminder";
        CharSequence contentText = "Reminder Content";
        Notification notification = new Notification(R.drawable.notif_icon,
                ticker, System.currentTimeMillis());
        Intent notificationIntent = new Intent(this,
                AlarmReceiverActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);
        // post notification
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(ns);
        final int HELLO_ID = 1;
        mNotificationManager.notify(HELLO_ID, notification);
    }
}

这篇关于称为AlarmManager通知活动不弹出,当应用程序被关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆