Android 系统和应用程序的重新启动

android系统烦人重新启动参考博客:

http://blog.sina.com.cn/s/blog_69b5d2a50101fvqp.html

http://blog.csdn.net/cuiran/article/details/19164817


一.android系统重启


1.用runtime来实现,即:

try 
{ 
    Runtime.getRuntime().exec("su"); 
    Runtime.getRuntime().exec("reboot"); 
} 
catch (IOException e) 
{ }

但是在自己身边的几个平板和手机设备都没有实现reboot,原因不明,看到也有很多人通过这个方法实现了设备的reboot。

2.用广播的形式:

public void reboot(Context context){
		LogsUtil.i(TAG, " reboot");
		Intent i = new Intent(Intent.ACTION_REBOOT); 
		i.putExtra("nowait", 1);     
		i.putExtra("interval", 1);  
		i.putExtra("window", 0);   
		context.sendBroadcast(i);
		LogsUtil.i(TAG, " reboot over");
	}

 

这个方法需要应用签名,很麻烦,换个设备就需要重新签名,我没有尝试。

3.参考“Android ICS关机跟重新启动功能研究一文

       Intent iReboot = new Intent(Intent.ACTION_REBOOT);      
        iReboot.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(iReboot);


这个方法一开始在4.2的平板上可以实现,以为没有问题了,结果在4.0的手机上报异常No activity founded to handle this intent。这个应该是android版本的问题了,没办法。

Powermanger

PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
pm.reboot(null);


尝试这个方法,在4.2和4.0的系统上都OK。

以上需要系统root权限,adb push xxx.apk system/app安装,一定要在这个目录下才能成为系统应用,来获取更大的操作权限


顺便说一下,系统关机的代码

Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, showShutdownDialog);
shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shutdown);


二.应用程序重启

//查找启动页,一般是SplashActivity
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //销毁所有的Activity,必须的哦
startActivity(i);


转载于:https://my.oschina.net/ososchina/blog/359550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值