Android timed Tasks

Source: Internet
Author: User

Ideas
    1. Invoke the scheduled task service in the main activity;
    2. Implement business logic within a timed task service "This example prints only one time log" and sends a message to alarm;
    3. Alarm Broadcast callback timer task service;

"2" "3" realizes closed loop loop.

Effect
 -- - Ten: -:45.461  10265-18533/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: -: $gmt+ ,:xx  - -- - Ten: -:50.522  10265-18637/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: -: -gmt+ ,:xx  - -- - Ten: -:55.545  10265-18774/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: -: -gmt+ ,:xx  - -- - Ten: $:00.587  10265-18900/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $:xxgmt+ ,:xx  - -- - Ten: $:05.617  10265-18998/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: togmt+ ,:xx  - -- - Ten: $:10.640  10265-19104/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $:Tengmt+ ,:xx  - -- - Ten: $:15.682  10265-19198/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: thegmt+ ,:xx  - -- - Ten: $:20.734  10265-19299/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: -gmt+ ,:xx  - -- - Ten: $:25.765  10265-19410/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: -gmt+ ,:xx  - -- - Ten: $:30.813  10265-19525/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: -gmt+ ,:xx  - -- - Ten: $:35.847  10265-19547/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: *gmt+ ,:xx  - -- - Ten: $:40.899  10265-19572/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: +gmt+ ,:xx  - -- - Ten: $:45.917  10265-19610/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: $gmt+ ,:xx  - -- - Ten: $:50.959  10265-19658/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: -gmt+ ,:xx  - -- - Ten: $:55.986  10265-19682/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: -gmt+ ,:xx  - -- - Ten: $:01.036  10265-19723/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: ongmt+ ,:xx  - -- - Ten: $:06.059  10265-19734/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: .gmt+ ,:xx  - -- - Ten: $:11.112  10265-19755/cf. ICBD. AboutserviceD/cbd:longrunningservice:mon Jul - Ten: $: Onegmt+ ,:xx  -
Mainactivity.java
publicclass MainActivity extends Activity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        new Intent(MainActivity.this , LongRunningService.class) ;        startService(intent) ;    }}
Longrunningservice.java
 Public  class longrunningservice extends Service {     Public Longrunningservice() {    }@Override     PublicIBinderOnbind(Intent Intent) {//Todo:return The communication channel to the service.        Throw NewUnsupportedoperationexception ("not yet implemented"); }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {NewThread (NewRunnable () {@Override                     Public void Run() {LOG.D ("CBD","Longrunningservice:"+NewDate (). toString ());        }}). Start ();        Alarmmanager Alarmmanager = (alarmmanager) getsystemservice (Alarm_service); Intent intent1 =NewIntent ( This, Alarmreceiver.class);//Trigger broadcast, broadcast callback this method, implement loopPendingintent pendingintent = Pendingintent.getbroadcast ( This,0, Intent1,0) ;LongTriggertime = Systemclock.elapsedrealtime () +5* +;//Trigger once every 5 secondsAlarmmanager.setexact (alarmmanager.elapsed_realtime_wakeup,triggertime,pendingintent);return Super. Onstartcommand (Intent, flags, Startid); }}
Alarmreceiver.java
publicclass AlarmReceiver extends BroadcastReceiver {    publicAlarmReceiver() {    }    @Override    publicvoidonReceive(Context context, Intent intent) {        new Intent(context, LongRunningService.class) ;        context.startService(i) ;    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android timed Tasks

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.