有誰知道如何在android中重新啓動服務?我有一個服務,當設備啓動時調用..和我有一個option.java保存我的配置..如何在android中重新啓動服務?
如果我編輯option.java中的配置,然後我必須重新啓動我的服務採取的效果。 。
我只知道如何啓動一個服務,運行後,我不知道如何重新啓動它後,新的配置..任何想法?
startService(new Intent(this, ListenSMSservice.class));
有誰知道如何在android中重新啓動服務?我有一個服務,當設備啓動時調用..和我有一個option.java保存我的配置..如何在android中重新啓動服務?
如果我編輯option.java中的配置,然後我必須重新啓動我的服務採取的效果。 。
我只知道如何啓動一個服務,運行後,我不知道如何重新啓動它後,新的配置..任何想法?
startService(new Intent(this, ListenSMSservice.class));
在您的元件:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2)在您的元件(一定要使用一個完全限定[或相對]類名稱爲您的廣播接收器):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
詳細說明如下:this
這是代碼是啓動服務的手機啓動?我想要如何重新啓動服務..任何想法尼克?謝謝.. –
當你重新啓動手機時,你的服務由此代碼自動啓動。 –
是的,但我想重新啓動服務時,服務已經重新啓動,而不是啓動..任何其他解決方案尼克?謝謝 –
所以按observer-observable design pattern,我的意思是利用FileObserver類由Android提供。
例如,這裏是從WallPaperManagerService.jav一個片段來自Android的源代碼:每次這個
所以,你的情況,你會在配置文件中創建一個文件觀察者(見下面的示例代碼),和配置文件更改,您將讀取(已經運行)服務中的所有值。
希望你有這個想法的本質。
/**
* Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
* that the wallpaper has changed. The CREATE is triggered when there is no
* wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
* everytime the wallpaper is changed.
*/
private final FileObserver mWallpaperObserver = new FileObserver(
WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
@Override
public void onEvent(int event, String path) {
if (path == null) {
return;
}
synchronized (mLock) {
// changing the wallpaper means we'll need to back up the new one
long origId = Binder.clearCallingIdentity();
BackupManager bm = new BackupManager(mContext);
bm.dataChanged();
Binder.restoreCallingIdentity(origId);
File changedFile = new File(WALLPAPER_DIR, path);
if (WALLPAPER_FILE.equals(changedFile)) {
notifyCallbacksLocked();
}
}
}
};
只要停止該服務,你爲什麼需要重新啓動服務以使更改生效再次啓動
stopService(new Intent(this, ListenSMSservice.class));
startService(new Intent(this, ListenSMSservice.class));
? – MrJre
只是出於好奇:保存配置後,爲什麼你的服務不能從同一個配置讀取?我的意思是,重新啓動服務需要什麼?如果你想重新讀取配置,爲什麼不能觀察者可觀察的模式? – Viren
@mrjre:當手機設置爲鎖定狀態(由SMS命令觸發)時,我將通過SMS發送報告給所有者的鎖屏應用程序(在通過選項表單在數據庫中註冊號碼),它會發送一個SMS報告給店主說他/她的設備已被鎖定。服務在電話啓動時啓動,所以如果我通過選項更改所有者編號,服務將不會檢測到新配置(新的所有者編號)並向舊編號發送短信報告,因爲服務在製作之前已經啓動一個新的配置。因此,我需要重新提供我的服務。任何想法? –