[英]How to set Reminder notification on Android?
我想在我的应用程序的android通知栏上显示所选日期和时间的提醒。但即使没有应用程序也应该填充一个。每当我的Android设备启动时,一个选定的时间有/无我的应用程序它应该显示通知在指定时间内如警报。 请帮帮我怎么做?
我找到了一些关于Alarm Manager,Notification manager的链接。 给我一个想法和一些链接,否则样本片段。
public static void notifyIcon(Context context){
NotificationManager notifier = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.your_app_notification, "", System.currentTimeMillis());
/**
*Setting these flags will stop clearing your icon
*from the status bar if the user does clear all
*notifications.
*/
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
notification.contentView = contentView;
//If you want to open any activity on the click of the icon.
Intent notificationIntent = new Intent(context, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);
notifier.notify(1, notification);
//To cancel the icon ...
notifier.cancel(1);
}
这是custom_notification_layout.xml
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp" >
</LinearLayout>
注意:请勿忘记在适当时清除应用程序图标如果您设置了上面的标记。
使用此按钮添加警报
Intent intent = new Intent(this, TheServiceYouWantToStart.class);
PendingIntent pending = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, Time_To_wake, pending);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.