我知道这个问题被问了很多次,但我没有找到任何解决方案来保持服务活跃,即使我的应用程序被杀死.我的应用程序在所有设备上运行,但某些设备如果我终止该应用程序,那么我的服务也会终止(设备名称 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 做了很多检查点即
然后我有一个名为 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.
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)上被杀死
发布评论