簡體   English   中英

服務在Android中的未知時間停止?

[英]Service Stops at an unknown time in android?

我是android的新手。 我正在制作一個應用程序,在該應用程序中,每隔指定的時間(例如15秒)必須重復執行某些任務。 我正在為此使用服務。 它可以在一段時間內正常工作,但是我的問題是,在不確定地知道原因之后,它會停止。 我正在使用帶有Timertask線程的Timer.Schedule。 請幫忙。 提前致謝。

您必須在onStart(Intent intent,int startId)內編寫實際代碼。 如果服務已經在后台運行,則Android不會在服務啟動時調用onStart方法,而是調用onStartCommand方法,將日志放入這兩個方法中,您將了解到。

public void onStart(Intent intent, int startId) {
    executeCode();
}

public int onStartCommand(Intent intent, int flags, int startId) {
    executeCode();

    return 0;
}

謝謝。

暫無
暫無

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

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