2017-02-13 155 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 –