android 定时关机需root权限
2016-09-30 00:00
10 查看
权限
AlarmManager定时任务
广播内容:
mainfest:
xml:
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
AlarmManager定时任务
import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import com.example.administrator.shutdowneasy.utils.SharePreferenceUtil; import java.util.Calendar; public class ShutDownInTime extends AppCompatActivity { private SharePreferenceUtil saveUtil; private TimePicker time=null; private TextView msg=null; private Button set=null; //private Button delete=null; private AlarmManager alarm=null; private int hourOfDay=0; private int minute=0; private Calendar cale=Calendar.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shut_down_in_time); saveUtil = new SharePreferenceUtil(this,"Time"); alarm=(AlarmManager)super.getSystemService(ALARM_SERVICE); set=(Button)super.findViewById(R.id.set); msg=(TextView)super.findViewById(R.id.msg); time=(TimePicker)super.findViewById(R.id.time); //delete=(Button)super.findViewById(R.id.delet); set.setOnClickListener(new setImp()); //delete.setOnClickListener(new deletImp()); time.setIs24HourView(true); time.setOnTimeChangedListener(new timeChange()); } private class timeChange implements TimePicker.OnTimeChangedListener { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { cale.setTimeInMillis(System.currentTimeMillis()); cale.set(Calendar.HOUR_OF_DAY,hourOfDay); cale.set(Calendar.MINUTE,minute); cale.set(Calendar.SECOND,0); cale.set(Calendar.MILLISECOND, 0); ShutDownInTime.this.hourOfDay=hourOfDay; ShutDownInTime.this.minute=minute; //set time is long } } private class setImp implements View.OnClickListener { @Override public void onClick(View v) { Intent it=new Intent(ShutDownInTime.this,AlarmBoast.class); it.setAction("com.example.administrator.shutdowneasy"); PendingIntent sender=PendingIntent.getBroadcast(ShutDownInTime.this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT); ShutDownInTime.this.alarm.set(AlarmManager.RTC_WAKEUP,ShutDownInTime.this.cale.getTimeInMillis(),sender); ShutDownInTime.this.msg.setText("您设置的自动关机时间是:"+ShutDownInTime.this.hourOfDay+":"+ShutDownInTime.this.minute); Toast.makeText(ShutDownInTime.this, "自动关机已设置成功", Toast.LENGTH_LONG).show(); ShutDownInTime.this.finish(); } } }
广播内容:
public class AlarmBoast extends BroadcastReceiver { public AlarmBoast() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. try { Process process = Runtime.getRuntime().exec("su"); DataOutputStream out = new DataOutputStream( process.getOutputStream()); out.writeBytes("poweroff -f\n");//可以为reboot -p\n out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage( e.getMessage()).setPositiveButton("OK", null).show(); } } }
mainfest:
<receiver android:name=".AlarmBoast" android:enabled="true" android:process=":remote"> <intent-filter> <action android:name="com.example.systemc" /> </intent-filter> </receiver>
xml:
<TimePicker android:id="@+id/time" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/time" android:text="没有设置关机时间"/> <Button android:id="@+id/set" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置关机时间" android:layout_below="@id/msg"/>
相关文章推荐
- android中定时-开“关机”的实现
- Android app调用系统重启、关机、获取root权限
- Android手机获取root权限并实现关机重启功能的方法
- android中定时-开“关机”的实现
- Android使用linux命令实现关机和重启---需要root权限
- 自动定时开机,关机
- 自己写的一个定时关机或重启的vbs脚本
- 用VC6.0实现定时关机功能
- WINXP/2000/2003系统定时关机小程序
- 为Ubuntu配置ssh服务,方便远程登陆 && ubuntu设置定时关机 && secureCRT显示彩色
- 蓝翼定时关机 免费
- Windows 2003 Server 定时关机
- 系统定时自动开、关机
- 怎样设置定时关机
- JAVA综合(系统信息,定时关机,计算器,日历,通讯录)
- linux 定时关机命令
- [vbscript]定时关机或重启的脚本,在windows 2000下通过
- 大学作品回顾--网络定时关机
- 小精灵定时关机 v1.0 绿色
- 电脑怎么定时关机和开机