android 定时关机

Calendar calendar = Calendar.getInstance();
long nowTime = calendar.getTimeInMillis();//这是当前的时间
int hour = 你要设置的时间
int minutes = 你要设置的时间

Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.HOUR_OF_DAY,hour);
myCal.set(Calendar.MINUTE,minutes);
long shutDownTime = myCal.getTimeInMillis();

Intent intent= new Intent();
intent.addAction();
PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

alarm.set(AlarmManager.RTC,shutDownTime -nowTime, pendingIntent);

下面是 service 。。。

接收到 Intent中的action 的话。。就启动。。

onStart(){
Intent intent = new Intent();
intent.setAction("android.intent.action.ACTION_SHUTDOWN");
sendBroadCast(intent);
}
权限

你可能感兴趣的:(android)