2017-02-13 113 views
1

Android服務一旦清除應用程序就會停止。即使用戶清除所有應用程序,我也需要一個在後臺持續運行的服務。 我創建了一個啓動服務的警報。Android服務永遠不會停止

public class AlarmReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Log.d("Alarm","Alarm receive"); 
    Intent i=new Intent(context,GetLocationService.class); 
    context.startService(i); 
    } 
} 

我的服務文件

public class GetLocationService extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(getApplicationContext(),"calling Get Location  service", Toast.LENGTH_LONG).show(); 
     //service code here 
     return Service.START_STICKY; 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d("Service","Service destroy"); 
    } 
} 

清單文件

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 


<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 

    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name=".GetLocationService" android:stopWithTask="false" android:exported="false" /> 

    <receiver android:name=".AlarmReceiver" android:enabled="true" /> 
</application> 

現在我需要跟蹤即使活動破壞移動位置.. 但對我來說,一旦我清楚應用程序我的服務被銷燬而不執行銷燬方法。我已閱讀STICKY_INTENT,但它不適合我。

+1

你測試哪些設備?一些設備不支持服務運行後,像在模擬器上測試的華爲和XIOMI –

+0

應用程序和華碩 –

+1

此鏈接可能會幫助您:http://stackoverflow.com/questions/15758980/android-service-needs-to-run永遠不會暫停或停止 – d1vivek681065

回答

0

使用stopSelf()內部服務,您摧毀服務

綁定服務文檔here

服務連接文檔here

例如綁定和服務連接here

+0

使用stopSelf()我的服務被停止了......但是我需要長時間運行的服務,它不應該被os破壞或應用程序 –

+0

如果你的服務不想銷燬意味着你有'綁定服務'和或使用'服務連接'通過這種方法長時間運行的服務,你可以防止銷燬 –

+0

服務是我的新主題。你能簡單地解釋一下還是給一些研究鏈接? –

0

這是剛剛的代碼解決辦法我不支持它。 因此,如果您的服務停止,它可以通過在Broad Cast接收器中註冊來重新啓動。 把這個在你的清單

<receiver android:name=".RestartTrigger"> 
      <intent-filter> 
       <action android:name="donotkill" /> 
      </intent-filter> 
     </receiver> 

而只是添加此類

public class RestartTrigger extends BroadcastReceiver { 
    private static final String TAG = "RestartServiceReceiver"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.e(TAG, "onReceive"); 
        Intent intent1 = Intent(context,GetLocationService.class); 
      context.startService(intent1); 


    } 
} 

而且在服務添加此

@Override 
    public void onDestroy() { 
     super.onDestroy(); 

      sendBroadcast(new Intent("donotkill")); 
    } 

而且在清單喲應該讓你的服務作爲獨立的進程添加android:process=":name_of_process"

<service android:name=".GetLocationService" android:stopWithTask="false" android:process=":name_of_process" android:exported="true" android:enabled="true" /> 

而我不支持這種類型的代碼。這將對用戶造成危害。

+0

它不會爲我工作。新進程也被殺死,一旦我清除應用內存 –

+0

@BipinGawand你有沒有發現一個應用程序不會被殺死後清除其數據?? :) –

1
+0

JobScheduler dnt支持21 api以下 –

+0

好的。檢查此鏈接https://github.com/Toolwiz/ToolWizAppLock/tree/master/src/com/cleanwiz/applock –