User Tools

Site Tools


android:settime-to-start-service

定时启动Service

利用AlarmManager启动Service.

  AlarmManager alarmManager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
            long initialDelay = System.currentTimeMillis() + 1 * 10 * 1000;
            Utils.log("initialDelay="+initialDelay);
            Intent i = new Intent();
            i.setAction(Intent.ACTION_SYNC);
            i.setClassName(getContext(), TwitterService.class.getName());
            PendingIntent intent = PendingIntent.getService(getContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.setInexactRepeating(AlarmManager.RTC, initialDelay, AlarmManager.INTERVAL_FIFTEEN_MINUTES, intent);

/var/www/dokuwiki/wiki/data/pages/android/settime-to-start-service.txt · Last modified: 2016/05/05 13:07 by 127.0.0.1