2011-12-22 58 views
1

我試圖做活動等待意向


你好,大家好,我試圖創造出了一個Intent啓動服務閃屏。在服務啓動到活動(SplashScreen)之後,應該等到它從我的服務中收到一個意向。

問題


什麼我需要做的,我的活動等待,直到它收到了我服務的意圖。如果你能爲我提供一個很好的教程或一些代碼片段,那將是很好的。 在這裏你可以找到我的SplashScreen代碼。

代碼


package de.stepforward; 

import de.stepforward.service.VideoService; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class SplashActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     // Sagt dem Video-Service das er beginnen soll die 
     // Videos herunter zu laden 
     Intent LoadVideos = new Intent(this, VideoService.class); 
     LoadVideos.putExtra("VIDEO_SERVICE", "start"); 
     startService(LoadVideos); 

     // In this thread I want that it waits for my Intent 
     // and than it goes to the next Activity 
     Thread splashThread = new Thread() { 

      @Override 
      public void run() { 
       try { 
        int waited = 0; 
        while (waited < 3000) { 
         sleep(100); 
         waited += 100; 
        } 
       } 
       catch (InterruptedException e) { 
        // do nothing 
       } 
       finally { 
        finish(); 
        final Intent Showvideo = new Intent(SplashActivity.this, 
                 ChannelTest.class); 
        startActivity(Showvideo); 
       } 
      } 
     }; 
     splashThread.start(); 
    } 
} 

回答

1

這裏是你的架構應該是什麼樣子:

INTENT_CST


String START_INIT_ACTION = "your.package.name.START_INIT"; 
String INIT_ENDED_ACTION = "your.package.name.INIT_ENDED"; 

SplashActivity


在的onCreate:

startService(new Intent(START_INIT_ACTION) 

在的onResume:
如果您選擇在您的服務發送一個廣播:

registerReceiver(new BroadcastReceiver(){ 
    //implement onChange()}, 
    new IntentFilter(INIT_ENDED_ACTION)); 

在的onPause,註銷您的接收器來釋放內存

LoadingService


Exten d AsyncTask做你的背景材料。在onPostExecute,2種選擇:

startActivity(new Intent(...)) // as your doing in your post 

sendBroadcast(new Intent(INIT_ENDED_ACTION)); stopSelf(); 

您的清單


聲明與IntentFilter<action name="your.package.name.START_INIT"/>

1

請參閱此鏈接可能對你有所幫助。您可以從您的活動中監控您的服務狀態。

Restful API service

0

啓動的服務不影響前景屏幕的.so啓動啓動畫面和啓動服務,因爲你正在做的事情。在服務中進行操作,然後開始新的活動形式服務本身。

+0

啊好主意服務LoadingService,但這是不可能的,飛濺creen等待意圖?之後,活動在SplashScreen上開始=) – safari 2011-12-22 08:11:16