我正在我的应用程序中运行遥测服务。该服务每秒检查一次WiFi环境中的更改,如果检测到更改,它将通知远程数据库。即使该应用程序已从最近的应用程序中刷过或关闭,我也需要此服务才能继续运行。
我关注了Continue Service even if application is cleared from Recent app,它似乎可以在除小米之外的许多设备上运行。我已经尝试了很多建议用于此设备的东西:
这是我最新的示例代码:
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");
}
}
它在Samsung设备和google nexus模拟器上运行良好,但是每当我在Red mi上运行并从最近的应用程序中刷出该应用程序时,该应用程序就会死机并提供服务。是否有针对Red mi设备的解决方案,或者这是众所周知的限制?
答案 0 :(得分:0)
小米的MIUI和其他几个在删除后台服务方面非常积极。作为用户,您可以在these steps (for xiaomi)之后禁用它。但是我不知道有什么解决方案可以绕开它作为开发人员。
因此,您没有做错任何事情,有些手机不喜欢消耗电池电量的后台服务。类似的讨论可以在这里找到:"Protected Apps" setting on Huawei phones, and how to handle it。他们的解决方案基本上是让用户通过为每个problematic devices
调用优化管理器来禁用它