AlarmManager调用的通知活动不会在应用关闭时弹出

时间:2012-10-04 14:42:08

标签: android notifications android-activity android-pendingintent

对于自定义提醒应用,我使用AlarmManagerPendingIntent设置我的Notification弹出的具体时间。

我在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会在指定时间弹出通知栏(左上角)中的通知,一切都很顺利。但它不是那么用户友好,我只希望通知栏显示在通知栏中,而不会在应用程序关闭时弹出任何活动(在现实情况下,可能在您的提醒到期时关闭)

1 个答案:

答案 0 :(得分:0)

定义AlarmManager

收到的PendingIntent BroadcastReceiverIntent 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);
    }
}

然后

{{1}}