【问题标题】:Background and foreground Service For Execution执行的后台和前台服务
【发布时间】:2020-01-19 05:45:55
【问题描述】:

我遇到了一个问题,我需要在后台计算一些并发计算,并在每次执行完成时通知用户。

我读了很多文章。我在其中找到了 Service Class、Intent Service 等。但我发现 24 个 android 操作系统版本以上崩溃。

如今在android中执行后台服务最好的方法是什么?

【问题讨论】:

  • 我们可以详细谈谈。如果你想要@Durgesh Kumar

标签: java android service background foreground


【解决方案1】:

对于上述问题,您可以使用工作管理器。为了更好地理解,请查看以下链接

https://developer.android.com/topic/libraries/architecture/workmanager

【讨论】:

    【解决方案2】:

    执行后台服务的最佳方法是使用系统AlarmManager 类并在每 XXX 秒后调用一次警报,但这会消耗更好的性能,但解​​决方案绝对适合您。 接下来的步骤,

    1. 创建警报
        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 从这样的主要活动中调用方法

    setUpalarm(MainActivity.this)
    

    【讨论】:

    • 我检查了这个解决方案,但在 Oreo 中,前台服务不会执行超过 5 秒。
    • 没有。前台服务将保留在那里。它只会在android收到三个以上通知时停止。Android删除最旧的通知。
    • 确保您在 manifest 中拥有前台权限以定位 android Oreo
    【解决方案3】:

    Android OREO 及更高版本可以使用前台服务 版本及以下 OREO Android 版本,您可以使用后台 STICKY、NON_STICKEY 服务。通过该服务,您可以在计算完成后广播您的操作并提供价值。

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2011-04-02
      • 2022-01-19
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2023-01-30
      相关资源
      最近更新 更多