1

对于自定义提醒应用程序,我正在使用 AlarmManager并设置弹出PendingIntent 的特定时间。Notification

我有我NotificationManagerReceiverActivity

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

4

1 回答 1

0

定义 要由 a 接收AlarmManagerPendingIntentBroadcastReceiver

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);
    }
}
于 2013-07-19T17:45:45.070 回答