This case knowledge is: The background to perform timed tasks.
Alarm mechanism:
First, create the Longrunningservice class
Package Com.example.servicebestpractice;import Java.util.date;import Android.app.alarmmanager;import Android.app.pendingintent;import Android.app.service;import Android.content.intent;import Android.os.IBinder; Import Android.os.systemclock;public class Longrunningservice extends Service {@Overridepublic IBinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {new Thread (new Runnable () {@Overridepublic void Run () {//print log simulates time-consuming operation. SYSTEM.OUT.PRINTLN ("Service startup time:" + new Date (). toString ())}). Start (); Alarmmanager manager = (Alarmmanager) getsystemservice (alarm_service); int times = 1000 * 60;//set how often to start a broadcast, I set it to 1 minutes to start a service, to perform timed tasks (although I'm writing a log that looks boring) long triggeratime = Systemclock.elapsedrealtime () + times;//set Trigger Point intent i = new Intent (this, alarmreceiver.class);//The Intent intent of the service to start the broadcast pendingintent pendingintent = Pendingintent.getbroadcast ( This, 0, I, 0);//Package Pendingintent, start the broadcast receiver Intent Manager.set (AlarmmanagEr. Elapsed_realtime_wakeup, triggeratime,pendingintent);//Set precise timing time, timed to trigger, broadcast start. Return Super.onstartcommand (Intent, flags, Startid);}}
Second, create to receive the above to start the broadcast.
Package Com.example.servicebestpractice;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;public class Alarmreceiver extends Broadcastreceiver {@Overridepublic void OnReceive ( Context context, Intent Intent) {//service class time to start the broadcast this method Intent Intent2 = new Intent (context, longrunningservice.class); Context.startservice (Intent2);//Start the broadcast to start the service operation, the service starts again. Because the service is no longer in the foreground, you do not need to set the Addflags () method. Because the service no longer uses the task stack to create. }}
Third, we want to have a start-up service entrance, then choose the main activity as the entrance:
Package Com.example.servicebestpractice;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;public class Mainactivity extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); The primary activity first has to start the service operation Intent intent3 = new Intent (this, longrunningservice.class); StartService (INTENT3);//Start Service } }
Iv. Broadcasting, activities, services three components Remember to configure the list file:
<activity android:name= "com.example.servicebestpractice.MainActivity" android:label= "@string/app_ Name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> < Category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name= "Com.example.servicebestpractice.LongRunningService" > </service> <receiver android:name= "Com.example.servicebestpractice.AlarmReceiver" > </receiver>
After I finished this blog, I looked at my log background output as follows:
Every minute, timed tasks are completed and a service is started.
Android Beginner Tutorial Start timer detailed