Android Services: START_STICKY does not work on Kitkat

前端 未结 5 1067
执笔经年
执笔经年 2021-01-31 20:02

I am using services in an application to listen for how many times the user presses his/her power button. The implementation was working fine on all devices. But when I tested t

5条回答
  •  温柔的废话
    2021-01-31 20:31

    If you are not able to restart the service then call a alam manger to start the reciver like this,

    Manifest is,

             
            
                
                    
                
            
    

    IN Main Activty start alarm manger in this way,

    String alarm = Context.ALARM_SERVICE;
            AlarmManager am = (AlarmManager) getSystemService(alarm);
    
            Intent intent = new Intent("REFRESH_THIS");
            PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0);
    
            int type = AlarmManager.RTC_WAKEUP;
            long interval = 1000 * 50;
    
            am.setInexactRepeating(type, System.currentTimeMillis(), interval, pi);
    

    this will call reciver and reciver is,

    public class AlarmReceiver extends BroadcastReceiver {
        Context context;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            this.context = context;
    
            System.out.println("Alarma Reciver Called");
    
            if (isMyServiceRunning(this.context, BackgroundService.class)) {
                System.out.println("alredy running no need to start again");
            } else {
                Intent background = new Intent(context, BackgroundService.class);
                context.startService(background);
            }
        }
    
        public static boolean isMyServiceRunning(Context context, Class serviceClass) {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List 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;
        }
    }
    

    And this Alaram reciver calls once when android app is opened and when app is closed.SO the service is like this,

    public class BackgroundService extends Service {
        private String LOG_TAG = null;
    
        @Override
        public void onCreate() {
            super.onCreate();
            LOG_TAG = "app_name";
            Log.i(LOG_TAG, "service created");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(LOG_TAG, "In onStartCommand");
            //ur actual code
            return START_STICKY;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // Wont be called as service is not bound
            Log.i(LOG_TAG, "In onBind");
            return null;
        }
    
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            super.onTaskRemoved(rootIntent);
            Log.i(LOG_TAG, "In onTaskRemoved");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.i(LOG_TAG, "In onDestroyed");
        }
    }
    

提交回复
热议问题