Android程序:系统重启+开机自动运行+屏幕解锁
android程序自动重启,释放内存。
1、系统自动重启方法
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(); } }
2、系统重启后程序自动运行
先在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);
}
}
3、程序启动时屏幕解锁
先在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);