赞
踩
android程序自动重启,释放内存。
private void shutdownDevice(Context context){ String cmd = "su -c reboot"; try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { // TODO Auto-generated catch block
new AlertDialog.Builder(context).setTitle("Error").setMessage( e.getMessage()).setPositiveButton("OK", null).show(); } }
先在AndroidManifest文件中添加权限和注册广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <receiver android:name=".BootBroadcastReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
添加BootBroadcastReceiver.class
- static final String ACTION = "android.intent.action.BOOT_COMPLETED";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION)){
- Intent sayHelloIntent=new Intent(context,activity.class);
-
- sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- context.startActivity(sayHelloIntent);
- }
- }
先在AndroidManifest文件中添加权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
onCreate中加入,最好在setContentView(R.layout.main)之前
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
- WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
- WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- setContentView(R.layout.main);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。