android - 中国Android设备杀死后台服务

标签 android android-studio service operating-system oppo

我正在尝试在 Android 应用程序中运行后台服务。我已经尝试过标准的方法和许多其他的调整。问题是,当应用程序从最近的应用程序中关闭时,该服务就会被终止。这种情况仅发生在 Oppo、Vivo、Xiomi 等中国 Android 设备上。在所有其他品牌上都可以正常工作。

我尝试过以下解决方案。

  1. 重写 Activity 的 OnStartCommand() 以返回 START_STICKY。在应用程序按预期关闭后,这仍然不会重新启动 Activity 。

    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            return START_STICKY;
        }
    
  2. 将应用程序添加到电池节省异常(exception)中,以便在 DOZE 模式下不会关闭它以节省电池。

  3. 从手机的安全设置中授予应用程序自动启动权限。

  4. 将服务作为前台服务启动,包括持久通知。

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
    
  5. 实现了在服务的 onTaskRemoved() 和 onDestroy() 方法中被终止后启动服务的警报管理器方法。

    <service
         android:name=".MyService"
         android:enabled="true"
         android:exported="true"
         android:stopWithTask="false" />
    
    @Override
        public void onTaskRemoved(Intent rootIntent){
            Log.d("Service:","I am being closed!");
            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);
        }
    
  6. 我还尝试使用广播管理器来启动我的服务,在 Activity 关闭时接收来自 Activity 的广播。

    <receiver
         android:name=".SensorRestarterBroadcastReceiver"
         android:enabled="true"
         android:exported="true"
         android:label="RestartServiceWhenStopped"
         />
    
    public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops!");
            context.startService(new Intent(context, MyService.class));;
        }
    }
    

但是,该服务不会在这些中国设备上再次启动。而Whatsapp、Facebook和一些著名应用程序的后台服务在关闭应用程序几分钟后就会恢复。请提出实现此目标的正确方法。

我已尝试 Background Service is not restarting after killed in oppo, vivo, mi android version 7.1.2 提供的解决方案也如上所述。这并不能解决我的问题。

最佳答案

这主要是为了优化电池和提高手机性能,

正如您尝试过我的 solution但在某些设备中您仍然会遇到此问题, 因此,这是从最近的应用程序列表中滑动时杀死应用程序的替代解决方案,

  1. 要求用户向下滑动应用程序,这会将应用程序锁定为白名单并且不会被杀死

或者

  • 为 list 中的每个 Activity 添加以下行,这将从最近的应用列表中隐藏您的应用。因此用户无法从最近的应用程序列表中滑动应用程序。

    android:excludeFromRecents="true"

  • 关于android - 中国Android设备杀死后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61027722/

    相关文章:

    java - 空指针异常变量

    Android:如何画圆角线?

    跨平台的 Android Studio git

    linux - centos7 : create service to start script bash

    android - 以编程方式将首选项添加到 PreferenceScreen 中的特定位置

    android - 具有多色的自定义 ProgressBar

    android - 如何从Native Android Activity获取数据到Flutter路由库?

    android - Flutter - 如何获取 TextField 值并将其显示为新文本

    android - 如何检查是否启用了定位服务?

    android - Android 中服务和使用警报管理器之间的区别