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
,但它不适合我。
你测试哪些设备?一些设备不支持服务运行后,像在模拟器上测试的华为和XIOMI –
应用程序和华硕 –
此链接可能会帮助您:http://stackoverflow.com/questions/15758980/android-service-needs-to-run永远不会暂停或停止 – d1vivek681065