Android服务永远不会停止
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
,但它不适合我。
这是刚刚的代码解决办法我不支持它。 因此,如果您的服务停止,它可以通过在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" />
而我不支持这种类型的代码。这将对用户造成危害。
它不会为我工作。新进程也被杀死,一旦我清除应用内存 –
@BipinGawand你有没有发现一个应用程序不会被杀死后清除其数据?? :) –
你应该看看的jobscheduler
这里有一些引用。
https://developer.android.com/reference/android/app/job/JobScheduler.html
http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
JobScheduler dnt支持21 api以下 –
好的。检查此链接https://github.com/Toolwiz/ToolWizAppLock/tree/master/src/com/cleanwiz/applock –
你测试哪些设备?一些设备不支持服务运行后,像在模拟器上测试的华为和XIOMI –
应用程序和华硕 –
此链接可能会帮助您:http://stackoverflow.com/questions/15758980/android-service-needs-to-run永远不会暂停或停止 – d1vivek681065