alarmManager经常启动服务(立即重复)

时间:2014-07-28 13:32:47

标签: android service alarmmanager

我的服务计划每15分钟启动一次,但它会在完成后立即重新启动(在真正的HTC one mini上)。同时,在仿真设备上不重复服务。 在我的活动中,我使用以下方法来scheldue alarm,它启动服务

private void enableService() {
    SharedPreferences times = PreferenceManager
            .getDefaultSharedPreferences(this);
    LastUpdateConstructor.INSTANCE.setTotalSeconds(times.getLong(
            "constructor", 0));
    LastUpdatePopular.INSTANCE.setTotalSeconds(times.getLong("popular", 0));
    LastUpdateActual.INSTANCE.setTotalSeconds(times.getLong("actual", 0));
    LastUpdateVypusk.INSTANCE.setTotalSeconds(times.getLong("vypusk", 0));
    // startService(new Intent(this, CheckForUpdateService.class));
    boolean alarmUp = (PendingIntent.getService(this, R.string.app_name,
            new Intent(SplashScreenActivity.this, CheckForUpdateService.class),
            PendingIntent.FLAG_NO_CREATE) != null);
    if (!alarmUp) {
        Log.e("new alarm", "was atached");
        Intent intent = new Intent(SplashScreenActivity.this, CheckForUpdateService.class);
        PendingIntent pintent = PendingIntent.getService(SplashScreenActivity.this,
                R.string.app_name, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_FIFTEEN_MINUTES, pintent);
    }
}

服务正在关注

    public class CheckForUpdateService extends Service {
        private WakeLock mWakeLock;

        @Override
        public IBinder onBind(Intent intent) {

            return null;
        }

        public void onCreate() {
            super.onCreate();

        }

        @Override
        public void onStart(Intent intent, int startId) {
            checkForUpdate();

        }

        public int onStartCommand(Intent intent, int flags, int startId) {
            checkForUpdate();
            return START_NOT_STICKY;
        }

        public Runnable check = new Runnable() {

            @Override
            public void run() {
                checkForUpdate();

            }
        };

        public void checkForUpdate() {
            Log.e("alarm", "alarm set");
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "predanie");
            mWakeLock.acquire();
            new GetAdds().execute();




        }
        public void onDestroy(){
            super.onDestroy();
            mWakeLock.release();
        }
    private class GetAdds extends AsyncTask<Void, Void, String> {
            @Override
            protected void onPreExecute() {

            }

            @Override
            protected String doInBackground(Void... params) {
                String message = "";
                String vypusk = checkVypusk();
                String popular = checkPopular();
                String actual = checkActual();
                String constructor = checkConstructor();
                message += vypusk;
                message += constructor;
                message += popular;
                message += actual;
                return message;

            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                if (!result.equals("")) {
                    postMessage(result);
                    Log.e("notification alarm", "alarm was fiered");
                } else {
                    Log.e("notification alarm", "alarm was triggered , but empty");
                }
                stopSelf();
            }

        }

}

我也尝试过使用日历作为某人,而我过去的时间已经确定了

private void enableService() {
        SharedPreferences times = PreferenceManager
                .getDefaultSharedPreferences(this);
        LastUpdateConstructor.INSTANCE.setTotalSeconds(times.getLong(
                "constructor", 0));
        LastUpdatePopular.INSTANCE.setTotalSeconds(times.getLong("popular", 0));
        LastUpdateActual.INSTANCE.setTotalSeconds(times.getLong("actual", 0));
        LastUpdateVypusk.INSTANCE.setTotalSeconds(times.getLong("vypusk", 0));
        // startService(new Intent(this, CheckForUpdateService.class));
        boolean alarmUp = (PendingIntent.getService(this, R.string.app_name,
                new Intent(SplashScreenActivity.this,
                        CheckForUpdateService.class),
                PendingIntent.FLAG_NO_CREATE) != null);
        if (!alarmUp) {
            Log.e("new alarm", "was atached");
            Intent intent = new Intent(SplashScreenActivity.this,
                    CheckForUpdateService.class);
            PendingIntent pintent = PendingIntent.getService(
                    SplashScreenActivity.this, R.string.app_name, intent, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.MINUTE, 2);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),
                    5*60*1000, pintent);
        }
    }

1 个答案:

答案 0 :(得分:0)

看来这个问题是由某种bug引起的。在我从计算机断开设备并重新启动它之后,服务在desiered interval中被调用