Use alarm to update earthquakes

Source: Internet
Author: User

 

In the earthquake example, you will use alarm to replace timer and perform network updates on a regular basis.

 

1. Create a New earthquakealarmreceiver class and extend broadcastreceiver.

 

Package com. paad. earthquake;

Import Android. content. broadcastreceiver;

Import Android. content. context;

Import Android. content. intent;

Public class earthquakealarmreceiver extends broadcastreceiver {

}

 

2. Override the onreceive method to explicitly start earthquakeservice.

 

@ Override

Public void onreceive (context, intent ){

Intent startintent = new intent (context, earthquakeservice. Class );

Context. startservice (startintent );

}

 

3. Create a new common static string to define the action used to trigger the broadcast receiver.

 

Public static final string action_refresh_earthquake_alarm =

"Com. paad. Earthquake. action_refresh_earthquake_alarm ";

 

4. Add a new earthquakealarmreceiver in manifest, which contains an intent-filter tag to listen for the action defined in step 1.

 

<Cycler Android: Name = ". earthquakealarmreceiver">

<Intent-filter>

<Action

Android: Name = "com. paad. Earthquake. action_refresh_earthquake_alarm"

/>

</Intent-filter>

</Cycler>

 

5. In earthquakeservice, update the oncreate method to obtain a reference of alarmmanager, create a new pendingintent, and send intent when alarm arrives. You can also remove the timertask initialization code.

 

Alarmmanager alarms;

Pendingintent alarmintent;

@ Override

Public void oncreate (){

Int icon = R. drawable. Icon;

String tickertext = "New Earthquake detected ";

Long when = system. currenttimemillis ();

Newearquakenotification = new notification (icon, tickertext, when );

Alarms = (alarmmanager) getsystemservice (context. alarm_service );

String alarm_action;

Alarm_action = earthquakealarmreceiver. action_refresh_earthquake_alarm;

Intent intenttofire = new intent (alarm_action );

Alarmintent = pendingintent. getbroadcast (this, 0, intenttofire, 0 );

}

 

6. Modify the onstart method to set an alarm, instead of using timer to regularly perform the next update operation (if the automatic update is enable ). Set a new intent with the same action to automatically cancel the previous alarm.

 

@ Override

Public void onstart (intent, int startid)

{

Sharedpreferences prefs = getsharedpreferences (preferences. user_preference, activity. mode_private );

Int minmagindex = prefs. getint (preferences. pref_min_mag, 0 );

If (minmagindex <0)

Minmagindex = 0;

Int freqindex = prefs. getint (preferences. pref_update_freq, 0 );

If (freqindex <0)

Freqindex = 0;

Boolean AutoUpdate = prefs. getboolean (preferences. pref_auto_update, false );

Resources r = getresources ();

Int [] minmagvalues = R. getintarray (R. array. magnes );

Int [] freqvalues = R. getintarray (R. array. update_freq_values );

Minimummagndomains = minmagvalues [minmagindex];

Int updatefreq = freqvalues [freqindex];

If (AutoUpdate)

{

Int alarmtype = alarmmanager. elapsed_realtime_wakeup;

Long timetorefresh = systemclock. elapsedrealtime () + updatefreq * 60*1000;

Alarms. Set (alarmtype, timetorefresh, alarmintent );

}

Else

Alarms. Cancel (alarmintent );

Refreshearthquakes ();

};

 

7. You can remove the updatetimer instance variables and timertask instances.

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.