【问题标题】:Continue service even when app is cleared from recent apps or closed on Red MI Device即使从最近的应用程序中清除应用程序或在 Red MI Device 上关闭应用程序,也可以继续服务
【发布时间】:2019-11-11 13:30:07
【问题描述】:

我正在我的应用中运行遥测服务。该服务每秒检查一次 WiFi 环境的变化,如果检测到变化,它会通知远程数据库。即使应用程序从最近的应用程序中滑动或关闭,我也需要此服务继续运行。

我关注了Continue Service even if application is cleared from Recent app,它似乎可以在除小米之外的许多设备上使用。我已经为这个设备尝试了很多建议:

  1. 启用自动启动。
  2. 禁用该应用的电池优化。
  3. 尝试使用广播接收器启动服务
  4. 尝试使用计时器线程和 scheduleExecutorservice 而不是可运行的。没有什么不同。

这是我最新的示例代码:

package com.example.csi1;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class MainService extends Service {
    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;
    public static int count = 0;
    public int restart = 0;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Toast.makeText(this, "MainService onStartCommand", Toast.LENGTH_LONG).show();
        Bundle extras = intent.getExtras();
        count = 0;

        handler = new Handler();
        runnable = new Runnable(){
            public void run() {
                count = count+1;

                WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo;

                wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
                    String ssid = wifiInfo.getSSID();
                    Log.i("Service", "SSID:"+ssid);
                    Toast toast = Toast.makeText(context, "Service iter " + String.valueOf(count)+ " " + ssid, Toast.LENGTH_LONG );
                    toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
                    toast.show();
                }

                handler.postDelayed(runnable, 1000);

            }
        };

        handler.postDelayed(runnable, 3000);
        return START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Toast.makeText(this, "onTaskRemoved", Toast.LENGTH_LONG).show();
        if (restart == 0) {
            PendingIntent service = PendingIntent.getService(
                    getApplicationContext(),
                    1001,
                    new Intent(getApplicationContext(), MainService.class),
                    PendingIntent.FLAG_ONE_SHOT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
            restart=1;
            Toast.makeText(this, "Service restarted", Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onDestroy(){
        Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
        if (restart == 0) {
            PendingIntent service = PendingIntent.getService(
                    getApplicationContext(),
                    1001,
                    new Intent(getApplicationContext(), MainService.class),
                    PendingIntent.FLAG_ONE_SHOT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
            restart=1;
            Toast.makeText(this, "Service restarted", Toast.LENGTH_LONG).show();
        }
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

它在三星设备和 google nexus 模拟器上运行良好,但每当我在 Red mi 上运行它并从最近的应用程序中滑动应用程序时,应用程序就会死掉并与之一起服务。是否有适用于 Red mi 设备的解决方案,或者这是一些众所周知的限制?

【问题讨论】:

    标签: java android-service redmi-device


    【解决方案1】:

    小米的 MIUI 和其他几个在删除后台服务方面非常激进。作为用户,您可以在these steps (for xiaomi) 之后禁用它。但作为开发人员,我不知道有什么解决方案可以规避它。

    所以你没有做错任何事,有些手机只是不喜欢后台服务占用电池寿命。可以在这里找到类似的讨论:"Protected Apps" setting on Huawei phones, and how to handle it。他们的解决方案基本上是让用户通过调用每个problematic devices的优化管理器来禁用它

    【讨论】:

    • 谢谢软件人。我已经完成了这些步骤,但服务仍然被杀死,所以我知道操作系统根本不会让我重新启动它?
    • 一般来说是的,但也许看看这个:stackoverflow.com/a/35220476/11702000。看看这是否有帮助。
    猜你喜欢
    • 2015-01-06
    • 2017-11-13
    • 2019-12-05
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    相关资源
    最近更新 更多