每当应用程序被杀死时服务停止
Service stop's whenever app is killed
START_STICKY 每当我终止我的应用程序然后服务不会再次启动时我的设备就无法工作,我的设备名称是 Redmi Note 3 Pro,但每当我 运行 相同的应用程序android 模拟器,当我终止应用程序时它会重新启动服务并且服务不会停止直到我通过 stopService() 方法停止它
请帮帮我
问题已解决
Done this:
设置>权限>自动启动
然后 打开我的应用程序的 Switch,然后完成!
我在这个 link 中找到了解决方案:
代码可能对您有帮助 HourlyService
class onStartCommand
方法 returns START_STICKY
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
里面Activity
private HourlyService mHourlyService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) iBinder;
mHourlyService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBound = false;
}
};
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, HourlyService.class);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(mServiceConnection);
mBound = false;
}
在某些设备(特别是小米、华为、联想)上,您需要将您的应用添加到 "protected apps" 或 "apps that are allowed to run in the background" 的列表中。如果您的应用不在列表中,Android 将不会自动重启您的 Service
,即使您已从 onStartCommand()
返回 START_STICKY
。这是一个 "battery saving feature" 不幸的是给开发人员带来了很多问题!
在电源管理、安全或应用下的 Android 设置中查找这些设置。
另请参阅:
- Xiaomi Redmi Note 3 custom service not working?
还请解释一下 "kill my app" 的含义。如果您强行关闭您的应用程序,那么 Service
将不会被 Android 重新启动。这是故意的,如果您强制关闭应用程序,它也不会在模拟器上重新启动。
您需要在服务属性里面的清单中添加"android:process=:any_name"。
例如,
<service android:name=".MyService"
android:process=":MyService"
android:enabled="true"/>
下面是我的服务代码class。
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("onCreate", "onCreate");
Toast.makeText(AppController.getInstance(),"Created",Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("servicedestroy", "servicedestroy");
Toast.makeText(AppController.getInstance(),"service destroy",Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(AppController.getInstance(),"Running",Toast.LENGTH_SHORT).show();
}
});
}
},
0,
5000);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
}
START_STICKY 每当我终止我的应用程序然后服务不会再次启动时我的设备就无法工作,我的设备名称是 Redmi Note 3 Pro,但每当我 运行 相同的应用程序android 模拟器,当我终止应用程序时它会重新启动服务并且服务不会停止直到我通过 stopService() 方法停止它
请帮帮我
问题已解决
Done this:
设置>权限>自动启动 然后 打开我的应用程序的 Switch,然后完成!
我在这个 link 中找到了解决方案:
代码可能对您有帮助 HourlyService
class onStartCommand
方法 returns START_STICKY
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
里面Activity
private HourlyService mHourlyService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) iBinder;
mHourlyService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBound = false;
}
};
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, HourlyService.class);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(mServiceConnection);
mBound = false;
}
在某些设备(特别是小米、华为、联想)上,您需要将您的应用添加到 "protected apps" 或 "apps that are allowed to run in the background" 的列表中。如果您的应用不在列表中,Android 将不会自动重启您的 Service
,即使您已从 onStartCommand()
返回 START_STICKY
。这是一个 "battery saving feature" 不幸的是给开发人员带来了很多问题!
在电源管理、安全或应用下的 Android 设置中查找这些设置。
另请参阅:
- Xiaomi Redmi Note 3 custom service not working?
还请解释一下 "kill my app" 的含义。如果您强行关闭您的应用程序,那么 Service
将不会被 Android 重新启动。这是故意的,如果您强制关闭应用程序,它也不会在模拟器上重新启动。
您需要在服务属性里面的清单中添加"android:process=:any_name"。
例如,
<service android:name=".MyService"
android:process=":MyService"
android:enabled="true"/>
下面是我的服务代码class。
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("onCreate", "onCreate");
Toast.makeText(AppController.getInstance(),"Created",Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("servicedestroy", "servicedestroy");
Toast.makeText(AppController.getInstance(),"service destroy",Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(AppController.getInstance(),"Running",Toast.LENGTH_SHORT).show();
}
});
}
},
0,
5000);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
}