【问题标题】:Android foreground service gets killed on MI 4 device ( version 5.0.2)Android 前台服务在 MI 4 设备(版本 5.0.2)上被杀死
【发布时间】:2017-01-04 19:40:25
【问题描述】:

我知道这个问题被问了很多次,但即使我的应用程序被杀死,我也没有找到任何解决方案来保持服务活着。 我的应用程序在所有设备上运行,但 某些设备 如果我终止了该应用程序,那么我的服务也会终止 (设备名称 MI 4 版本 ans asus 5.0.3)

以下是我启动前台服务的服务代码

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
 Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("My service started at ")
                    .setTicker("My service started")
                    .setContentText("app")
                    .setSmallIcon(R.drawable.app)
                    .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                    .setContentIntent(pendingIntent)
                    .setOngoing(true).build();
            startForeground(Constants.FOREGROUND_SERVICE,notification); 
}

【问题讨论】:

  • 这是 MIUI,放松。它的工作方式与 Android 不同。
  • @VladMatvienko 有什么解决办法吗
  • 我个人认为这不是我(也不是你)的问题。我们不是那些应该为那些奇怪而愚蠢的 MIUI 变化而受苦的人。我能给出的唯一建议是每隔 5 秒检查一次由 AlarmManager 运行的服务,如果它关闭,则启动它。
  • @VladMatvienko 谢谢你的回答
  • @VladMatvienko 我的 AlarmManager 也被杀死了

标签: android service


【解决方案1】:

对于这个问题,我在各种事件上为我的 TestService 设置了许多检查点 即

  1. NetworkChangeReceiver
  2. 引导接收器
  3. MainActivity 的 onCreate 方法

然后我有一个名为 ServiceDetector.java 的类

import android.app.ActivityManager;
import android.content.Context;
import java.util.List;
public class ServiceDetector {
    // this method is very important
    public boolean isServiceRunning(Context context, Class<?> serviceClass) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        if (services != null) {
            for (int i = 0; i < services.size(); i++) {
                if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {
                    return true;
                }
            }
        }
        return false;
    }
}

检查我的服务是否正在运行,现在如果您的服务没有运行,请重新启动它

public void onReceive(Context context, Intent intent) {
        ServiceDetector serviceDetector = new ServiceDetector();
        if (!serviceDetector.isServiceRunning(context, TestService.class)) {
            Intent startServiceIntent = new Intent(context, TestService.class);
            context.startService(startServiceIntent);
        } else {
            Log.i(Constants.TAG, "Service is already running reboot");
        }
    }

【讨论】:

    【解决方案2】:

    您可以实施的一种解决方法是在服务被删除时重新启动它。即,使用 onTaskRemoved 回调 (link)。

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        // TODO Auto-generated method stub
        Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2012-08-26
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多