2011-11-01 26 views
10

我有一個應用程序,我收到一個包含他的位置的短信。在接收短信時,它會調用另一個活動來啓動,並將該位置傳遞給該活動以在地圖上繪製它。在調用第二個活動之前,它會顯示一個敬酒就像屏幕上的通知,但由於調用第二次活動,烤麪包沒有出現,所以有些尷尬。我的問題是,我們該如何推遲從此活動中調用第二個活動?如何在從另一個活動調用活動時出現延遲?

回答

39

您可以使用像這樣做:

new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() { 

          Intent i=new Intent(SearxhJobs.this,JobsTypes.class); 
          startActivity(i); 
         } 
        }, 5000); 

這等待高達5秒的發射活動。

希望它有幫助

2

製作一個在doInBackground()方法中執行Thread.sleep()的AsyncClass,然後導航到onPostExecute()方法中的新活動。

調用你的toast消息,然後執行AsyncClass。

5

您可以用Handler這樣

Handler h = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 

      Intent i = new Intent().setClass(ctx, MainActivity.class);     
      startActivity(i); 
     }   
    }; 

    h.sendEmptyMessageDelayed(0, 1500); // 1500 is time in miliseconds 
0

只需設置佈局!

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 

      setContentView(R.layout.next); //where <next> is you target activity :) 

      } 
     }, 5000); 
1

嘗試:

Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      // if you are redirecting from a fragment then use getActivity() as the context. 
      startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
      // To close the CurrentActitity, r.g. SpalshActivity 
      finish(); 
     } 
}; 

Handler h = new Handler(); 
// The Runnable will be executed after the given delay time 
h.postDelayed(r, 1500); // will be delayed for 1.5 seconds 
0

一個例子是以下幾點:

Handler TimeDelay=new Handler(); 
       if(previous=="geofence"){ 



        tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null); 
        Runnable r = new Runnable() { 
         @Override 
         public void run() { 
          /* 
          Intent intent = new Intent(
            MyBroadcastMessageReceiver.class.getName()); 
          intent.putExtra("some additional data", choice); 
          someActivity.sendBroadcast(intent);*/ 
          tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null); 
         } 
        }; 
        TimeDelay.postDelayed(r, 150000); 
相關問題