2012-08-06 35 views

回答

0

singleTask是您需要的活動,以防止它被加載多次。

要啓動設備上的某些東西,您需要聽取啓動信息;

public class BootReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     startService(this, new Intent(Intent.ACTION_SYNC, null, this, SomeIntentService.class); 
    } 
} 

具有類似的清單;

<receiver android:name="BootReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

但是如果你想要長時間運行任何進程,你需要一個前臺服務;

http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification)

對如何讓您的服務爲一體的持續性狀態奇妙的文檔。 IntentService會很簡單,您只需要重寫一個方法http://developer.android.com/reference/android/app/IntentService.html#onStartCommand(android.content.Intent,int,int)

然後將數據存儲在sharedpreferences或數據庫中,然後您的活動訪問數據存儲。

+0

謝謝!聲音good.Just驗證我明白:你建議在開機時我的活動將啓動一個IntentService,當用戶點擊圖標時,活動將(自動)被銷燬,然後在服務仍在運行時啓動? – Andy 2012-08-06 07:00:01

+0

是的! :)因此,直擊你的活動,所有的工作都應該在服務中完成。該活動僅通過持久性存儲監視服務。 – 2012-08-06 07:02:38

相關問題