2017-04-25 53 views

回答

0

您可以使用AlarmManager來執行操作:

AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
alarmIntent.putExtra("Activity",1); 
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, pendingIntent); 
alarmIntent = new Intent(this, AlarmReceiver.class); 
alarmIntent.putExtra("Activity",2); 
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2000, pendingIntent); 
alarmIntent = new Intent(this, AlarmReceiver.class); 
alarmIntent.putExtra("Activity",3); 
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, pendingIntent); 

AlarmReceiver類

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); 
     int extra = intent.getExtra("Activity"); 
     switch(extra) { 
       case 1: 
        //start 1st activity 

       case 2: 
        //start 2nd activity 

       case 3: 
        //start 3rd activity 
     } 

    } 
} 
+0

是影響性能? – user3231661

+0

它不會影響應用程序的性能,但如果屏幕進入睡眠狀態,它會喚醒屏幕。所以,如果你沒有這樣的要求,那麼你可以使用正常的等待意圖 – Sonam

0

我正在使用處理程序

Handler handler = new Handler(); handler.postDelayed(新的Runnable(){

 @Override 
     public void run() { 
      // TODO Auto-generated method stub 

     } 
    }, 1000); 

    handler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

     } 
    }, 2000); 
    handler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

     } 
    }, 3000); 
}