开发者

How to set Reminder notification on Android?

开发者 https://www.devze.com 2023-04-01 20:00 出处:网络
i want to display reminder on selected date and time on notification bar of android from my application .But that one should be populated even without the application.Whenever My android devices boote

i want to display reminder on selected date and time on notification bar of android from my application .But that one should be populated even without the application.Whenever My android devices booted up and one the selected time with/without my application it should display notification in specified time l开发者_JS百科ike alarm. please help me how to do this?.

I found some links about Alarm Manager,Notification manager. Give me the idea and some links otherwise sample snippet.


Use this to add an alarm

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);


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);

}

Here is 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>

Note: Don't forget to clear your app icon when appropriate If you set above flag.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号