欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 實(shí)現(xiàn)定時(shí)任務(wù)的過程詳解

 更新時(shí)間:2020年06月19日 10:04:19   投稿:mrr  
這篇文章主要介紹了Android 定時(shí)任務(wù)過程詳解的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在Android開發(fā)中,通過以下三種方法定時(shí)執(zhí)行任務(wù):

一、采用Handler與線程的sleep(long)方法(不建議使用,java的實(shí)現(xiàn)方式)

二、采用Handler的postDelayed(Runnable, long)方法(最簡(jiǎn)單的android實(shí)現(xiàn))

三、采用Handler與timer及TimerTask結(jié)合的方法(比較多的任務(wù)時(shí)建議使用)

android里有時(shí)需要定時(shí)循環(huán)執(zhí)行某段代碼,或者需要在某個(gè)時(shí)間點(diǎn)執(zhí)行某段代碼,這個(gè)需求大家第一時(shí)間會(huì)想到Timer對(duì)象,沒錯(cuò),不過我們還有更好的選擇。

 一、Timer 實(shí)現(xiàn)定時(shí)任務(wù)

Timer timer;
void onCreate(){
 ......
TimerTask task = new TimerTask(){ 
public void run(){ 
 // 在此處添加執(zhí)行的代碼 
} 
}; 
timer = new Timer(); 
timer.schedule(task, 1000);//開啟定時(shí)器,delay 1s后執(zhí)行task 
}
void onDestroy(){
......
timer.cancel();//銷毀定時(shí)器
}

二、Handler實(shí)現(xiàn)定時(shí)任務(wù)

1.隔一段時(shí)間后執(zhí)行某個(gè)操作,循環(huán)執(zhí)行:

void onCreate(){ 
 ......
 Handler handler = new Handler(); 
 Runnable runnable = new Runnable(){ 
  @Override 
  public void run() { 
  // TODO Auto-generated method stub 
  // 在此處添加執(zhí)行的代碼 
  handler.postDelayed(this, 50);// 50ms后執(zhí)行this,即runable 
  } 
 }; 
 handler.postDelayed(runnable, 50);// 打開定時(shí)器,50ms后執(zhí)行runnable操作 
}
void onDestroy(){ 
 ......
 handler.removeCallbacks(this);// 關(guān)閉定時(shí)器處理 
}

2.隔一段時(shí)間后執(zhí)行某個(gè)操作一次,執(zhí)行完后,不再執(zhí)行:

void onCreate(){ 
......
Handler handler = new Handler();  
 Runnable runnable = new Runnable(){ 
 @Override 
 public void run() { 
  // TODO Auto-generated method stub  
  // 在此處添加執(zhí)行的代碼 
              doSomeThing();
  handler.removeCallbacks(this); //移除定時(shí)任務(wù) 
   }  
 }; 
 handler.postDelayed(runnable, 50);// 打開定時(shí)器,50ms后執(zhí)行runnable 
}

三、AlarmManager實(shí)現(xiàn)精確定時(shí)操作

 我們使用Timer或者h(yuǎn)andler的時(shí)候會(huì)發(fā)現(xiàn),delay時(shí)間并沒有那么準(zhǔn)。如果我們需要一個(gè)嚴(yán)格準(zhǔn)時(shí)的定時(shí)操作,那么就要用到AlarmManager,AlarmManager對(duì)象配合Intent使用,可以定時(shí)的開啟一個(gè)Activity,發(fā)送一個(gè)BroadCast,或者開啟一個(gè)Service.

下面的代碼詳細(xì)的介紹了兩種定時(shí)方式的使用:

在指定時(shí)長(zhǎng)后執(zhí)行某項(xiàng)操作

// 以下的代碼是<<足球即時(shí)比分>>中的代碼片段.
 public static AlarmManagerUtil{
   public static AlarmManager getAlarmManager(Context ctx){
   return (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
  }
  /**
  * 指定時(shí)間后進(jìn)行更新賽事信息(有如鬧鐘的設(shè)置)
  * 注意: Receiver記得在manifest.xml中注冊(cè)
   * 
  * @param ctx
  */
  public static void sendUpdateBroadcast(Context ctx){
   Log.i("score", "send to start update broadcase,delay time :"+);
   larmManager am = getAlarmManager(ctx);
    // 秒后將產(chǎn)生廣播,觸發(fā)UpdateReceiver的執(zhí)行,這個(gè)方法才是真正的更新數(shù)據(jù)的操作主要代碼
   Intent i = new Intent(ctx, UpdateReceiver.class); 
   PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, , i, );
   am.set(AlarmManager.RTC, System.currentTimeMillis()+, pendingIntent)
 }
  /**
  * 取消定時(shí)執(zhí)行(有如鬧鐘的取消)
  * 
  * @param ctx
  */  
  public static void cancelUpdateBroadcast(Context ctx){
   AlarmManager am = getAlarmManager(ctx);
   Intent i = new Intent(ctx, UpdateReceiver.class);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, , i, );
   am.cancel(pendingIntent);
  }
 }
 // 更新數(shù)據(jù)庫的廣播接收器
 public static class UpdateReceiver extends BroadcastReceiver{
   public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "更新比分?jǐn)?shù)據(jù)", Toast.LENGTH_LONG).show();
    // 設(shè)置全局定時(shí)器(鬧鐘) 秒后再發(fā)廣播通知本廣播接收器觸發(fā)執(zhí)行.
    // 這種方式很像JavaScript中的 setTimeout(xxx,)
    AlarmManagerUtil.sendUpdateBroadcast(context);
   }
  }

周期性的執(zhí)行某項(xiàng)操作

publicstaticvoid sendUpdateBroadcastRepeat(Context ctx){
 Intent intent =new Intent(ctx, UpdateReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
 //開始時(shí)間
 long firstime=SystemClock.elapsedRealtime();
 AlarmManager am = (AlarmManager) ctx.getSystemService(ALARM_SERVICE);
  //60秒一個(gè)周期,不停的發(fā)送廣播
 am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 60*1000, pendingIntent);
}

取消定時(shí)器(鬧鐘)

/**
 * 取消定時(shí)執(zhí)行(有如鬧鐘的取消)
 * 
 * @param ctx
 */publicstaticvoid cancelUpdateBroadcast(Context ctx){
  AlarmManager am = getAlarmManager(ctx);
  // 取消時(shí)注意UpdateReceiver.class必須與設(shè)置時(shí)一致,這樣才要正確取消
  Intent i = new Intent(ctx, UpdateReceiver.class); 
  PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, i, 0);
  am.cancel(pendingIntent);
 }
}

以上所述是小編給大家介紹的Android 定時(shí)任務(wù)過程詳解,希望大家喜歡。

相關(guān)文章

最新評(píng)論