1

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

即使从最近的应用程序中清除了应用程序,我也关注了继续服务,并且它似乎可以在除小米以外的许多设备上运行。我已经为这个设备尝试了很多建议:

  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 设备的解决方案,或者这是一些众所周知的限制?

4

1 回答 1

0

小米的 MIUI 和其他几个在删除后台服务方面非常积极。作为用户,您可以按照以下步骤禁用它(适用于小米)。但我不知道作为开发人员规避它的解决方案。

所以你没有做错任何事,有些手机只是不喜欢后台服务占用电池寿命。类似的讨论可以在这里找到:华为手机上的“受保护的应用程序”设置,以及如何处理。他们的解决方案基本上是让用户通过为每个有问题的设备调用优化管理器来禁用它

于 2019-06-30T19:25:47.767 回答