簡體   English   中英

Android onStartCommand服務

[英]Android onStartCommand Service

只要用戶打開了我的服務,我就會嘗試使其持續運行。 問題在於服務使用了大量的內存。 我猜是因為它會通知用戶並不斷顯示圖像。 因此,我將大部分代碼放在了另一個活動中。 該服務僅調用活動。

問題是我試圖在需要時使用return Start_Sticky重新啟動服務。 它需要大約2個小時才能用盡足夠的內存來重新啟動。 當它重新啟動時,它不會執行onStartCommand嗎?

public class AUTOAlarmService extends Service {
@Override
public void onCreate() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Intent DI = new Intent(getBaseContext(), AUTOSERVICES.class);
    DI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplication().startActivity(DI);
    return START_STICKY;
}

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}

}

我認為您應該重新考慮已完成的某些工作,因為您的服務占用了太多資源,並且不應該以任何方式發生。 有許多應用程序具有很多服務,但與此無關,沒有重啟設備的問題。

但是,如果要重新啟動服務,則必須先停止然后重新啟動該服務。

stopService(new Intent(this, YourService.class));
startService(new Intent(this, YourService.class));

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM