gpt4 book ai didi

android - 中国安卓设备杀后台服务

转载 作者:行者123 更新时间:2023-12-04 10:16:54 25 4
gpt4 key购买 nike

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

我尝试了以下解决方案。

  • 凌驾于OnStartCommand() Activity 返回START_STICKY .在应用程序按预期关闭后,这仍然不会重新启动 Activity 。
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
    }
  • 将应用程序添加到省电异常(exception),以便在DOZE 中不会关闭它来省电模式。
  • 将应用程序权限授予 Auto-Start从手机的安全设置。
  • 将服务作为前台服务启动,包括持久通知。
    @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;
    }
  • 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);
    }
  • 我还尝试使用广播管理器来启动我的服务,当它关闭时从 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但您仍然在某些设备中遇到此问题,
    所以这是从最近的应用程序列表中滑动杀死应用程序的替代解决方案,

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

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

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

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com