我有一项服务,只有在应用程序未运行时才能运行。我在用户关闭应用程序时启动它,并在用户打开应用程序时将其停止。 当我安装应用程序时,如果我关闭应用程序,服务会立即启动,这没关系。当我打开应用程序时,服务停止(我可以从被调用的服务中看到onDestroy方法)但是立即自动重新启动,没有任何指令。
在我的AndroidManifest.xml中,我有:
<receiver android:name=".BootReceiver" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" />
这是'因为我需要在重启设备时启动服务
在我的服务中我有
public class MyService extends Service {
Thread _thisThread=null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//code to execute when the service is first created
}
@Override
public void onDestroy() {
//code to execute when the service is shutting down
Log.v("App","MyService - OnDestroy method called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("App","MyService - onStartCommand");
//return Service.START_NOT_STICKY;
return Service.START_STICKY;
}
此致,如果我停止服务,我无法理解为什么服务会自动启动。