如果应用被终止,服务不会 运行
The service does not run if the app is killed
所以我有一项服务可以检查屏幕是打开还是关闭(来自 here)。
这是主要的 activity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onCreate();
}
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
protected void onPause() {
if (ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED OFF");
} else {
System.out.println("this is when onPause() is called when the screen state has not changed ");
}
super.onPause();
}
@Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED ON");
} else {
System.out.println(" this is when onResume() is called when the screen state has not changed ");
}
super.onResume();
}
}
服务class:
public class UpdateService extends Service {
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
System.out.println("Screen is off");
} else {
System.out.println("Screen is on");
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
接收者:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive ");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED OFF" );
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
System.out.println("SCREEN TURNED ON on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED ON" );
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
当应用程序被发送到后台时,上述服务运行完美。但是当使用任务管理器终止该应用程序时,该服务将不再工作。即使应用程序被终止,我也需要该服务才能工作。
我应该怎么做才能启用它?有什么想法吗?
如果他们运行在同一个进程中,很明显服务和你的桌面应用程序一起终止了。
运行 您的服务作为 Windows 服务 / Linux 守护进程,与用户创建的进程相比,它们具有不同的生命周期。例如,您的应用程序将在注销时自动终止。
原因是您的服务运行与应用程序在同一个进程上。如果您在清单文件中设置android:process=":yourServiceName"
,您的服务将启动一个新进程,您可以看到您的服务。
此外,请使用此方法
检查您的服务是否确实 运行
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
要重新启动服务,您可以在服务的 onTaskRemoved 方法中使用 AlarmManager。
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);
}
所以我有一项服务可以检查屏幕是打开还是关闭(来自 here)。 这是主要的 activity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onCreate();
}
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
protected void onPause() {
if (ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED OFF");
} else {
System.out.println("this is when onPause() is called when the screen state has not changed ");
}
super.onPause();
}
@Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED ON");
} else {
System.out.println(" this is when onResume() is called when the screen state has not changed ");
}
super.onResume();
}
}
服务class:
public class UpdateService extends Service {
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
System.out.println("Screen is off");
} else {
System.out.println("Screen is on");
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
接收者:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive ");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED OFF" );
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
System.out.println("SCREEN TURNED ON on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED ON" );
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
当应用程序被发送到后台时,上述服务运行完美。但是当使用任务管理器终止该应用程序时,该服务将不再工作。即使应用程序被终止,我也需要该服务才能工作。
我应该怎么做才能启用它?有什么想法吗?
如果他们运行在同一个进程中,很明显服务和你的桌面应用程序一起终止了。
运行 您的服务作为 Windows 服务 / Linux 守护进程,与用户创建的进程相比,它们具有不同的生命周期。例如,您的应用程序将在注销时自动终止。
原因是您的服务运行与应用程序在同一个进程上。如果您在清单文件中设置android:process=":yourServiceName"
,您的服务将启动一个新进程,您可以看到您的服务。
此外,请使用此方法
检查您的服务是否确实 运行private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
要重新启动服务,您可以在服务的 onTaskRemoved 方法中使用 AlarmManager。
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);
}