我想在我的应用程序的android通知栏上显示选定日期和时间的提醒。但是即使没有应用程序,也应该填充该提醒。每当我的android设备启动并且一个选择的时间有/没有我的应用程序时,它应该在指定的时间内显示通知,如报警。 请帮我怎么做到这一点?如何在Android上设置提醒通知?
我发现了一些关于Alarm Manager,Notification Manager的链接。给我的想法和一些链接,否则样本片段。
我想在我的应用程序的android通知栏上显示选定日期和时间的提醒。但是即使没有应用程序,也应该填充该提醒。每当我的android设备启动并且一个选择的时间有/没有我的应用程序时,它应该在指定的时间内显示通知,如报警。 请帮我怎么做到这一点?如何在Android上设置提醒通知?
我发现了一些关于Alarm Manager,Notification Manager的链接。给我的想法和一些链接,否则样本片段。
使用此增加一个报警
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);
如果我从我的应用程序出来,上述代码不起作用。我希望警报服务能够工作没有我的appl也。 – ADIT
是的这将工作...它将启动'TheServiceYouWantToStart' 你必须添加代码onStartCommand –
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>
注:请不要忘记在适当的时候如果上述标志设置为清除您的应用程序图标。
写的教程。通知提醒:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ – Blundell