Android 前台服务在 MI 4 设备(版本 5.0.2)上被杀死

编程入门 行业动态 更新时间:2024-10-28 12:20:08
本文介绍了Android 前台服务在 MI 4 设备(版本 5.0.2)上被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

@Overridepublic int onStartCommand(意图意图,int标志,int startId){通知通知 = new NotificationCompat.Builder(this).setContentTitle("我的服务开始于").setTicker("我的服务已启动").setContentText("应用程序").setSmallIcon(R.drawable.app).setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pendingIntent).setOngoing(true).build();startForeground(Constants.FOREGROUND_SERVICE,notification);}

解决方案

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

  • NetworkChangeReceiver
  • 启动接收器
  • MainActivity 的 onCreate 方法
  • 然后我有一个名为 ServiceDetector.java 的类

    import android.app.ActivityManager;导入 android.content.Context;导入 java.util.List;公共类 ServiceDetector {//这个方法很重要public boolean isServiceRunning(Context context, Class serviceClass) {ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);列表服务 = activityManager.getRunningServices(Integer.MAX_VALUE);如果(服务!= null){for (int i = 0; i < services.size(); i++) {if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {返回真;}}}返回假;}}

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

    public void onReceive(Context context, Intent intent) {ServiceDetector serviceDetector = new ServiceDetector();如果 (!serviceDetector.isServiceRunning(context, TestService.class)) {Intent startServiceIntent = new Intent(context, TestService.class);context.startService(startServiceIntent);} 别的 {Log.i(Constants.TAG, "服务已经在运行重启");}}

    I know this question is asked so many times but i didn't find any solution to keep service alive even if my app get killed. My application run on all devices but some devices if i kill the app then my service also killed (Device name MI 4 Version ans asus 5.0.3 )

    following is my service code where i have started my foreground service

    @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); }

    解决方案

    For this problem, i make many checkpoints for my TestService on various events i.e.

  • NetworkChangeReceiver
  • BootReceiver
  • onCreate method of MainActivity
  • Then i have a class called 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; } }

    Which checks if my service is running, Now if your service is not running then start it again

    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"); } }

    更多推荐

    Android 前台服务在 MI 4 设备(版本 5.0.2)上被杀死

    本文发布于:2023-11-27 05:51:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1636850.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:被杀   前台   版本   设备   Android

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!