"Original" Android local push

Source: Internet
Author: User

Android local push Implementation principle: Open a broadcastreceiver and a alarmmanager, alarm set the push wake-up time, Broadcastreceiver has been detecting whether should push.

at present, a lot of mobile phone shutdown application service was killed, unable to accept the push. Various restart service I also tried the millet phone is not good to make! If you make sure the service is not dead, receive push

public static String pushaction = "cn.xxx. Pushaction ";

Pushdata= "1|2|09:50| content ^2|2|09:58| Content"//id| type | time | content

Set a repeating alarm

Sharedpreferences sharedpreferences = Cocos2dxactivity.getcontext (). Getsharedpreferences ("SP", Cocos2dxactivity.mode_private);
Sharedpreferences.editor Editor = Sharedpreferences.edit ();
Editor.putstring ("Key", pushdata);
Editor.commit ();

Intent Intent =new Intent (Cocos2dxactivity.getcontext (), pushreceiver.class);
Intent.setaction (pushaction);
Pendingintent Sender=pendingintent.getbroadcast (Cocos2dxactivity.getcontext (), intent, Pendingintent.flag_ Cancel_current);
Alarmmanager alarm= (Alarmmanager) Cocos2dxactivity.getcontext (). Getsystemservice (Context.alarm_service);
Alarm.setrepeating (Alarmmanager.rtc,system.currenttimemillis (), 60*1000, sender); --set to send pushaction every other minute set to repeat execution

Set the alarm type once

Long T = Long.parselong (time) *1000+system.currenttimemillis ();
Intent Intent =new Intent (Cocos2dxactivity.getcontext (), pushreceiver.class);
Intent.setaction (pushaction);
Intent.putextra ("id", id);--note that this ID is the best one, if you set multiple push, ID must be unique or disorderly
Intent.putextra ("content", body);
Intent.putextra ("type", 2); corresponding Pushreceiver type judgment
Pendingintent Sender=pendingintent.getbroadcast (Cocos2dxactivity.getcontext (), ID, intent, Pendingintent.flag_ Cancel_current); -note that the second parameter must be unique when there are multiple pushes
Alarmmanager alarm= (Alarmmanager) Cocos2dxactivity.getcontext (). Getsystemservice (Context.alarm_service);
Alarm.set (ALARMMANAGER.RTC, T, sender);--triggers a push from the current start interval

Implementation of trigger Push Pushreceiver class

@Override
public void OnReceive (Context arg0, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (Push.pushaction))
{
Pushnotify (arg0); Set up repetitive pushes
if (Intent.getintextra ("type", 0) ==2) {//corresponds to the type in the previous push type
SendNotify1 (Intent.getintextra ("id", 0), Intent.getstringextra ("content"), arg0);
}
}
}


public static void Pushnotify (Context ctx) {
Sharedpreferences sharedpreferences = ctx.getsharedpreferences ("SP", cocos2dxactivity.mode_private);
String con = sharedpreferences.getstring ("Key", "");
LOG.E ("eeee", con);
String temp[] = Con.split ("\\^");
if (temp.length<=0) return;
Int week =calendar.getinstance (). get (Calendar.day_of_week);

int hour =calendar.getinstance (). get (Calendar.hour_of_day);
String strhour = "";
if (hour<=9)
{
Strhour = "0" +hour;
}
else {
Strhour = hour+ "";
}

int mimute = Calendar.getinstance (). get (Calendar.minute);
String strmimute= "";
if (mimute<=9) {
Strmimute = "0" +mimute;
}
else {
Strmimute = mimute+ "";
}
for (int i=0;i<temp.length;i++)
{
String pushstr[] = Temp[i].split ("\\|");
int id = integer.parseint (pushstr[0]);
int type = Integer.parseint (pushstr[1]);
String time = pushstr[2];
String content = pushstr[3];
Switch (type) {
Case 2://Set a few minutes to push
String t =strhour+ ":" +strmimute;
if (Time.equals (t)) {
SendNotify1 (ID, content,ctx);
}
Break
Case 3://Days of the week and a few minutes of push
int tempweek = 0;
Switch (week) {
Case 1:
Tempweek = 7;
Break
Case 2:
Tempweek = 1;
Break
Case 3:
Tempweek = 2;
Break
Case 4:
Tempweek = 3;
Break
Case 5:
Tempweek = 4;
Break
Case 6:
Tempweek = 5;
Break
Case 7:
Tempweek = 6;
Break

Default
Break
}
String T1 =tempweek+ ":" +strhour+ ":" +strmimute;
if (time.equals (t1)) {
SendNotify1 (ID, content,ctx);
}
Week = 0;
Break
Default
Break
}
}

}

@SuppressWarnings ("deprecation")//Set push
public static void SendNotify1 (final int id,final String body,final Context ctx)
{
Notificationmanager nm = (Notificationmanager) ctx.getsystemservice (Context.notification_service);

Notification Noti = new Notification (R.drawable.icon, Body,system.currenttimemillis ());

Noti.defaults = Notification.default_sound;

String title = ctx.getstring (R.string.app_name);

Noti.flags = Notification.flag_auto_cancel;

Intent Intent = new Intent (CTX, Pokemon.class);

Intent.setflags (intent.flag_activity_clear_top| Intent.flag_activity_new_task);

Pendingintent contentintent = pendingintent.getactivity (CTX, id,intent, pendingintent.flag_update_current);

Noti.setlatesteventinfo (Ctx,title, body, contentintent);

Nm.notify (ID, Noti);
}

Androidmanifest.xml Configuration

<receiver android:name= "cn.xxx. Pushreceiver ">
<intent-filter android:priority = "$" >
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
<action android:name= "Android.intent.action.USER_PRESENT"/>
<action android:name= "cn.xxx. Pushaction "/>
</intent-filter>
</receiver>

"Original" Android local push

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.