我正在通過服務和異步任務內運行後臺任務。首先,它從數據庫獲取所有數據,並在用戶更改某些選項時開始運行,我銷燬並再次啓動服務以處理新數據,但不幸的是,服務從停止的位置運行。所以我想開始全新的服務。我試過START_NOT_STICKY
但沒用。我可以在android中完全重新啓動服務嗎?
public class MyService extends Service {
public void onCreate(){
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
process.execute();
return START_NOT_STICKY;
}
public void onDestroy() {
stopSelf();
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
class ProcessImageAsync extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// here im getting content from db and processing .
if (isCancelled()) {
System.exit(0);
}
}
}
}
我的活動在這裏的代碼是如何重新啓動服務
public void startServiceFucntion(int type){
if(isMyServiceRunning(MyService.class)){
if (type == 1){
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
startServiceFucntion(0);
}
} else {
Intent serviceIntent = new Intent(this, MyService.class);
if(serviceReceiver == null){
registerReceiver(serviceReceiver, intentSFilter);
}
startService(serviceIntent);
}
}
謝謝你的哥們。我不知道我是如何錯過它的。 – Thamaraiselvam
歡迎兄弟:)有時它也發生在我身上 –