Android communications demo

Source: Internet
Author: User
Tags time in milliseconds

Principles and demos of notifications for Android

Notifications are often used in apps. This is a common feature. For example, if the Netease news client has any major news, a notification will pop up in the notification bar.

In progressProgramI also encountered this requirement during the process. A notification is automatically displayed every 7 days to remind users. I searched the internet and it took two days to complete the search. The implementation process is as follows:

I. The alarm function must be called for notification. The first step is to set the alarm.

/* Parameter 1: context parameter 2: wake-up time (in milliseconds)

* Function: sends an alarm notification.

Public static void setalarmtime (context, long timeinmillis ){
Alarmmanager AM = (alarmmanager) Context. getsystemservice (context. alarm_service );
Intent intent = new intent ("android. Alarm. Demo. Action ");
Pendingintent sender = pendingintent. getbroadcast (context, 0, intent, pendingintent. flag_cancel_current );
Int interval = 7*24*60*60*1000; // The format of the 7-day time in milliseconds
Am. setrepeating (alarmmanager. rtc_wakeup, timeinmillis,

Interval, Sender); // parameter 2 indicates the wake-up time, and parameter 2 indicates the time interval.

}

You need to call this method where necessary, and then store the time used for the call. Why.

StorageCode:

Sharedpreferences alarmtime = context. getsharedpreferences (constant. alarm_time, 0 );
Editor editor = alarmtime. Edit ();
Editor. putlong ("thefirstnotifybegintime", system. currenttimemillis ());
Editor. Commit ();

2. Receive the broadcast from step 1.

Public class alarmreceiver extends broadcastreceiver {

@ override
Public void onreceive (context, intent) {
// todo auto-generated method stub
If (" android. alarm. demo. action ". equals (intent. getaction () {

// call the notification operation here
policyutil. addappnotify (context); // for my own notification functions, see Step 3
sharedpreferences alarmtime = context. getsharedpreferences ("alarm_time", 0); // Why is this happening? Just read it.
editor = alarmtime. edit ();
editor. putlong ("lastpolicytime", system. currenttimemillis ();
editor. commit ();
return;
}
}

}

3: The notification function to be called in step 2.

Public class policyutil {

Public static void addappnotify (context ){
Icationicationmanager manager = (notificationmanager) Context. getsystemservice (context. icationication_service );
Notification = new notification ();
Notification. Icon = R. drawable. icon_app_notify; // notification icon
Notification. tickertext = context. getresources (). getstring (R. String. app_tickertext); // notification content
Notification. defaults = notification. default_sound; // notification ringtone
Notification. audiostreamtype = Android. Media. audiomanager. adjust_lower;
Notification. Flags | = notification. flag_auto_cancel;

Intent intent = new intent ();
Intent. setcomponent (New componentname ("carman.exe cise ","Carman.exe cise. Main"));
Intent. setflags (intent. flag_activity_new_task );
Pendingintent = pendingintent. getactivity (context, 0, intent, pendingintent. flag_one_shot );
// Click the icon in the status bar to set the prompt information
Notification. setlatesteventinfo (context, context. getresources (). getstring (R. String. app_policy_title ),
Context. getresources (). getstring (R. String. app_notify), pendingintent );
Manager. Policy (1, notification); // The first parameter in this function indicates identifier. To bring up multiple notifications at the same time, the parameter of each notification must be different.

Otherwise, the subsequent notifications will overwrite the previous notifications.
}
}

The above steps can implement the notification function, but a restart listener is missing. Otherwise, the alarm will become invalid once the phone restarts.

4. Restart and recover the alarm, which is achieved through a broadcast.

After the restart, the Set alert clock becomes invalid. You must use this broadcast to recalculate the next response time nextpolicytime and call Step 1 again.

Public class bootreceiver extends broadcastreceiver {

@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
String action = intent. getaction ();
If (action. Equals (intent. action_boot_completed )){
Sharedpreferences alarmtime = context. getsharedpreferences ("alarm_time", 0); // data storage preferences
Long lastpolicytime = alarmtime. getlong ("lastpolicytime", 0); // obtain the time when the last alarm is triggered, that is, the value stored in step 2.
Long nextpolicytime;
If (lastpolicytime = 0 ){
Long thefirstnotifybegintime = alarmtime. getlong ("thefirstnotifybegintime", 0); // if no notification is displayed, obtain the value stored in step 1.
Nextpolicytime = 7*24*60*60*1000 + thefirst?ybegintime;
} Else {
Nextpolicytime = 7*24*60*60*1000 + lastpolicytime;
}
If (nextpolicytime <= system. currenttimemillis ()){
Notifyutil. setalarmtime (context, system. currenttimemillis ());
} Else {
Notifyutil. setalarmtime (context, nextpolicytime); // set it as an alarm again
}
}
}
}

5: After the debugging is completed, the debugging still fails, because you need to register the broadcast receiver in androidmanifest. As follows:

The receiver in step 2 listens to the alarm

<Cycler Android: Name = ". Communications. alarmreceiver">
<Intent-filter>
<Action Android: Name = "android. Alarm. Demo. Action"/>
</Intent-filter>
</Cycler>

The receiver in Step 4 listens for restart
<Cycler Android: Name = ". Communications. bootreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>

The yellow font is the Code related to the alarm restart listener.

It can be reproduced, but please indicate the source. Thank you!

Author: Carman 20:10:57

Email: carman_loneliness@163.com

If you need the source code, you can leave an email address for "comment.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.