相关疑难解决方法(0)

前景服务从奥利奥被杀

我编写了一个前台服务,它适用于低于Oreo的所有操作系统版本.从Oreo开始,应用程序流程在关闭5分钟后被杀死,并从最近删除应用程序.

根据android开发人员文档中的后台执行限制, 操作系统不应该终止正在运行前台服务的应用程序,并且通知窗口中会显示通知.

根据开发人员文档的指导原则.我按照以下步骤启动前台服务.

  1. 前台服务以startForegroundService()方法开始
  2. 在启动服务后的5秒内,将显示使用该服务的通知 startForeground()
  3. 返回START_STICKY从onStartCommand()的service

我在以下手机上遇到此问题:

  1. OnePlus 5T
  2. 体内
  3. OPPO
  4. 弥

我试图阻止前台服务被破坏?

  1. 通过向用户显示系统对话框以禁用打盹模式,禁用应用程序的电池优化.

我试图重启前台服务?

  1. 用于AlarmManager从onTaskRemoved()重新启动服务.请查看此链接了解详情.

根据我的理解,这些制造商已经定制了AOSP,并且不遵守允许前台服务运行的操作系统指南.可能这些制造商已经做到了这一点,因为它给用户带来了很长的电池续航时间

前台服务课

    class DataCaptureService : Service() {

        private var isServiceStarted = false

        override fun onBind(intent: Intent?): IBinder? {
            return null
        }

override fun onCreate() {
        super.onCreate()
        wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                    newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakelockTag123").apply {
                        acquire()
                    }
                }
    }


        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int …
Run Code Online (Sandbox Code Playgroud)

android android-source android-service

11
推荐指数
3
解决办法
6735
查看次数

前台通知服务在一加设备中不起作用

下面是启动前台服务的代码。它适用于许多设备,如三星、moto、Vivo、Oppo 以及 Android 版本的牛轧糖和奥利奥,但不适用于一加设备。任何人都可以告诉我是否需要任何额外的更改或许可才能在一加设备上运行,或者是否有任何模拟器支持一加手机。

public class ForegroundService extends Service {

private Context ctx;
private static final String PRIMARY_NOTIF_CHANNEL = "default";

@Override
public void onCreate() {
    super.onCreate();
    ctx = this;
    createNotificationService();
}

private void createNotificationService(){

    TelephonyManager mTelephonyManager = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
    if(mTelephonyManager != null)
        mTelephonyManager.listen(new CellTowerStateListener(ctx), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setupChannel();
    }

    Notification notification = new NotificationCompat.Builder(this, …
Run Code Online (Sandbox Code Playgroud)

android foregroundnotification foreground-service oneplusone oneplusthree

5
推荐指数
1
解决办法
2163
查看次数