Android 關機 重啓設備

1.重啓安卓系統需要系統權限,需要把應用設置成系統應用

設置成系統應用有很多種方法,我這裏用最簡單的一種,用系統簽名生成系統應用。

不知道如何設置的 可以看看這篇文章 

https://blog.csdn.net/qq_34115898/article/details/103511810

2.然後在xml 添加權限

  android:sharedUserId="android.uid.system"
 
  <uses-permission android:name="android.permission.SHUTDOWN/>
 
  <uses-permission android:name="android.permission.REBOOT"

3.1關機代碼

Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
        intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);

 

3.2 重啓代碼

   PowerManager pm = (PowerManager) this.context.getSystemService(Context.POWER_SERVICE);
   pm.reboot("");

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章