2016-09-12 25 views
0

即使清除正在運行的應用程序(RAM)後,如何連續運行Android服務...請爲我提供服務代碼,而不是重新給我Android安全代碼 我的代碼已給出如下:我該如何連續運行Android服務

public class MyService extends Service { 
public MyService() { 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    return null; 
} 


@Override 
public int onStartCommand(Intent intent1, int flags, int startId) { 
    // do your jobs here 
    return super.onStartCommand(intent1, flags, startId); 
} 

回答

0

你需要從onStartCommand根據自己的需要在回報START_STICKYSTART_REDELIVER_INTENT

public class MyService extends Service { 
    public MyService() { 
    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
      // TODO: Return the communication channel to the service. 
      return null; 
    } 


    @Override 
    public int onStartCommand(Intent intent1, int flags, int startId) { 
      // do your jobs here 
      return START_STICKY; 
    } 

} 

正如開發者網站上標明,如果你不打算通過任何意圖啓動該服務,然後簡單地返回START_STICKY標誌。這將用空白的意圖對象重新創建服務。

0

你必須返回服務爲粘稠,這樣

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY; 
} 
相關問題