Skip to content

Instantly share code, notes, and snippets.

@easternHong
Created October 6, 2014 03:05
Show Gist options
  • Save easternHong/751061344daa2a316945 to your computer and use it in GitHub Desktop.
Save easternHong/751061344daa2a316945 to your computer and use it in GitHub Desktop.
protect Service
private void startSelfService() {
// 获得系统级别服务
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent in = new Intent(this, this.getClass());
PendingIntent pin = PendingIntent.getService(this, 0, in,
PendingIntent.FLAG_UPDATE_CURRENT);
// 返回系统开机到现在的时间差
long timePeriod = SystemClock.elapsedRealtime();
// ELAPSED_REALTIME =3s
am.setRepeating(AlarmManager.ELAPSED_REALTIME, timePeriod, 3 * 1000,
pin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment