执行的后台和前台服务
Background and foreground Service For Execution
我遇到了一个问题,我需要在后台计算一些并发计算,并在每次执行完成时通知用户。
我看了很多文章。我在其中找到了 Service Class、Intent Service 等。但我发现 24 android os 版本以上崩溃。
Nowadays which method is best to execute the background service in android and why?
以上问题可以使用工作管理器。为了更好地理解,请查看以下内容 link
https://developer.android.com/topic/libraries/architecture/workmanager
执行后台服务的最佳方法是使用系统 AlarmManager
class 并在每 XXX 秒后调用警报,但它会耗尽电池电量,但解决方案肯定适合您。
要遵循的步骤,
- 创建警报
public static void setUpalarm(Context context) {
Intent intent = new Intent(context, RestartServiceFromTimer.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(context , 0,
intent,0);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
long delay = 5 * 1000 * 60; // time sets to 5 minute change accordingly
long time = System.currentTimeMillis() + delay;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
alarm.set(AlarmManager.RTC_WAKEUP, time, pIntent);
else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
alarm.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pIntent);
}
2 创建广播接收器并重新启动服务并再次安排警报
public class RestartServiceFromTimer extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Task Trigger","Task is triggered");
if(!isMyServiceRunning(DetectIncomonCallService.class,context.getApplicationContext()))
{ Intent myserviceIntent = new Intent(context.getApplicationContext(),Service.class);
//start your background service here
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
ContextCompat.startForegroundService(context.getApplicationContext(),myserviceIntent);//this is for forground service
}
else
{
context.startService(myserviceIntent);
}
}
MainActivity.setUpalarm(context.getApplicationContext());
}
private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
3 像这样从 main activity 调用方法
setUpalarm(MainActivity.this)
You can use Foreground Service for android OREO and higher
versions and for below OREO Android version, you can use background STICKY, NON_STICKEY services. From the service, you can broadcast your action with value when your calculation is completed.
我遇到了一个问题,我需要在后台计算一些并发计算,并在每次执行完成时通知用户。
我看了很多文章。我在其中找到了 Service Class、Intent Service 等。但我发现 24 android os 版本以上崩溃。
Nowadays which method is best to execute the background service in android and why?
以上问题可以使用工作管理器。为了更好地理解,请查看以下内容 link
https://developer.android.com/topic/libraries/architecture/workmanager
执行后台服务的最佳方法是使用系统 AlarmManager
class 并在每 XXX 秒后调用警报,但它会耗尽电池电量,但解决方案肯定适合您。
要遵循的步骤,
- 创建警报
public static void setUpalarm(Context context) {
Intent intent = new Intent(context, RestartServiceFromTimer.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(context , 0,
intent,0);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
long delay = 5 * 1000 * 60; // time sets to 5 minute change accordingly
long time = System.currentTimeMillis() + delay;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
alarm.set(AlarmManager.RTC_WAKEUP, time, pIntent);
else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
alarm.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pIntent);
}
2 创建广播接收器并重新启动服务并再次安排警报
public class RestartServiceFromTimer extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Task Trigger","Task is triggered");
if(!isMyServiceRunning(DetectIncomonCallService.class,context.getApplicationContext()))
{ Intent myserviceIntent = new Intent(context.getApplicationContext(),Service.class);
//start your background service here
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
ContextCompat.startForegroundService(context.getApplicationContext(),myserviceIntent);//this is for forground service
}
else
{
context.startService(myserviceIntent);
}
}
MainActivity.setUpalarm(context.getApplicationContext());
}
private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
3 像这样从 main activity 调用方法
setUpalarm(MainActivity.this)
You can use Foreground Service for android OREO and higher versions and for below OREO Android version, you can use background STICKY, NON_STICKEY services. From the service, you can broadcast your action with value when your calculation is completed.