Android重启应用和重启手机

重启应用

private void restartApp() {  
        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());  
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        startActivity(intent);  
} 

RunTime重启手机

 case R.id.shutdown_btn3:
                try{
                    Log.v(TAG, "root Runtime->shutdown");
                    //Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","shutdown"});
                    Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","reboot -p"});
                    proc.waitFor();
                }catch(Exception e){
                    e.printStackTrace();
                }
                break;
            case R.id.reboot_btn3:
                try { 
                    Log.v(TAG, "root Runtime->reboot");
                    Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","reboot "});
                    proc.waitFor();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
                break;

反射调用重启手机

try {

     //获得ServiceManager类
     Class<!--?--> ServiceManager = Class.forName("android.os.ServiceManager");
     //获得ServiceManager的getService方法
     Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
     //调用getService获取RemoteService
     Object oRemoteService = getService.invoke(null,Context.POWER_SERVICE);
     //获得IPowerManager.Stub类
     Class<!--?--> cStub = Class.forName("android.os.IPowerManager$Stub");
     //获得asInterface方法
     Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
     //调用asInterface方法获取IPowerManager对象
     Object oIPowerManager = asInterface.invoke(null, oRemoteService);
     //获得shutdown()方法
     Method shutdown = oIPowerManager.getClass().getMethod("shutdown",boolean.class,boolean.class);
                        //调用shutdown()方法
                        shutdown.invoke(oIPowerManager,false,true);           
     } catch (Exception e) {         
         Log.e(TAG, e.toString(), e);        
     }

广播重启手机

            //广播方式关机重启
            case R.id.shutdown_btn1:
                Log.v(TAG, "broadcast->shutdown");
                Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
                intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
                //其中false换成true,会弹出是否关机的确认窗口
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.reboot_btn1:
                Log.v(TAG, "broadcast->reboot");
                Intent intent2 = new Intent(Intent.ACTION_REBOOT);
                intent2.putExtra("nowait", 1);
                intent2.putExtra("interval", 1);
                intent2.putExtra("window", 0);
                sendBroadcast(intent2);  
                break;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值