Periodically obtaining/updating and comparing data in android? -
I am creating an Android application that provides local weather forecasts from weather APIs as well as Twitter updates on Aurora Borealis and both Correlates I was wondering what is the best way to update and compare my constant data, even if the user does not have an app on their device? Therefore, I essentially want to run my app in the background. Any help would be great!
You can periodically repeat the task that is implemented in an IntentService. / P>
Public Class MyService Intent Service {Public MySiva () {Super ("MySive"); } @ Override Protected Zero to Handle Intent {New My Task ()) carry about (); Log D. ("MyService", "About performing My Task"); } Private class MyTask AsyncTask and lt; String, zero, boolean & gt; {@Override protected boolean doInBackground (string ... strings) {log d. ("MyService", "Calling Iinbackground in My Task"); return false; }}}
You need to set a periodic alarm through the alarm manager, which allows you to decide that when you use a pending intentant; For example, in an activity:
... int interval = 10 * 1000; // every 10 seconds alarmsmajor alarm = (alarmmanager) this.getSystemService (context.ALARM_SERVICE); Intent alarmart = new intent (this, MyAlarm receiver class); Pending pending vertical positioning = Pending. Gentlestrokes (this, 0, alarm, 0); Alarm.SetInxpect Reparating (alarm manager .rc_wecup, systemcontentime milliseconds), interval, pending implantation; ...
Pending notification will be sent to listen for a broadcast receiver for alarm:
Public class expands MyAlarmReceiver BroadcastReceiver {public MyAlarmReceiver () {} Override public null at @release (reference reference, intent to intent) {intent service intent = new intent (reference, MySwire class); Context.startService (serviceIntent); Log D. ("MyAlarm Receiver", "References Called. Starters Service"); The phone turns off every day, the alarm set by you is automatically canceled, so you need a broadcast receiver to reset the alarm: Public Class Mayuboot Receiver Broadcast Receiver Expands (Public MyBoot Receiver () {} UpperRead Public Records on Receive (Context Reference, Intention of Intent) {Log D. ("Mayuboot Resource", "Boot Complete"); Inter Interval = 10 * 1000; // every 10 seconds alarmsmajor alarm = (alarm manager) context.getSystemService (event .ALARM_SERVICE); Intent AlarmArtent = new intent (reference, MyAlarm receiver class); Pending pending vertical = present = pending .gatebrastro (reference, 0 Alarmantant, 0); Alarm.SetInxact Reparating (Alarm Manager. RTC_wecup, System.contentime milliseconds), Interval, Pending Transplantation;}}
In AndroidManifest.xml, you will find RECEIVE_BOOT_COMPLETED Need to add BOOT_COMPLETED Intent-Filter to the Motion and Broadcast Receiver:
& gt; ... & lt; Receiver Android: name = ". Services.MyBootReceiver" Android: enabled = "true" android: exported = "true" & gt; & Lt; Intro-Filter Android: Priority = "500" & gt; & Lt; Action Android: name = "android.intent.action.BOOT_COMPLETED" /> & Lt; / Intent-Filter & gt; & Lt; / Receiver & gt; ... & lt; / Application & gt; ... & lt; Usage-permission Android: name = "android.permission.RECEIVE_BOOT_COMPLETED" />
More detail
Comments
Post a Comment