为什么我的应用会抛出“android.permission.REBOOT SecurityException”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3456467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:16:43  来源:igfitidea点击:

Why does my app throw an `android.permission.REBOOT SecurityException`?

androidpermissionsreboot

提问by oscar

I wrote an Android app that uses android.os.PowerManager.reboot(), and I added <uses-permission android:name="android.permission.REBOOT" />in my AndroidManifest.xml. However, when I run the app, it always throws the following exception:

我编写了一个使用 的 Android 应用程序android.os.PowerManager.reboot(),并<uses-permission android:name="android.permission.REBOOT" />在我的AndroidManifest.xml. 但是,当我运行该应用程序时,它总是抛出以下异常:

java.lang.SecurityException: Neither user 10039 nor current process has android.permission.REBOOT. at android.os.Parcel.readException(Parcel.java:1247)
at android.os.Parcel.readException(Parcel.java:1247)
at android.os.Parcel.readException(Parcel.java:1235)
at android.os.IPowerManager$Stub$Proxy.reboot(IPowerManager.java:427)
at android.os.PowerManager.reboot(PowerManager.java:481)
at Test.testPower(Test.java:374)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
at android.os.Parcel.readException(Parcel.java:1247)
at android.os.Parcel.readException(Parcel.java:1235)
at android.os.IPowerManager$Stub$Proxy.reboot(IPowerManager.java:427)
at android.os.PowerManager.reboot(PowerManager.java:481)
at com.fsl.cts.FSLPlaybackTest.testPower(FSLPlaybackTest.java:374)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

Is the anything I'm doing wrong, or does froyo have some problem with rebooting?

是我做错了什么,还是 froyo 重启有问题?

回答by Sarwar Erfan

You cannot get this permission. Only system applications and applications signed with the same key that was used to sign the firmware will be able to get that permission.

您无法获得此许可。只有使用用于签署固件的相同密钥签署的系统应用程序和应用程序才能获得该许可。

Why do you need to reboot? If you tell the necessity of rebooting, then may be someone can suggest you a better way to accomplish the same thing without rebooting.

为什么需要重启?如果您告诉重启的必要性,那么可能有人可以建议您在不重启的情况下完成同样的事情的更好方法。

回答by Aleadam

It can't be done in stock android phones. But, as I posted in other similar question, if the phone is rooted, then you have no problem at all:

它不能在库存安卓手机中完成。但是,正如我在其他类似问题中发布的那样,如果手机已植根,那么您根本没有问题:

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

For an explanation, follow the link above.

有关解释,请点击上面的链接。

回答by DonGru

As far as I know this permission can only be grated to system apps, "user applications" can't get that permission

据我所知这个权限只能磨碎给系统应用程序,“用户应用程序”无法获得该权限

回答by Archit

To reboot the device using code needs REBOOT permission.

使用代码重启设备需要 REBOOT 权限。

This permission is available for system applications. You can make your application a system application only if you have the certificate used in building the system image.

此权限可用于系统应用程序。只有当您拥有用于构建系统映像的证书时,您才能使您的应用程序成为系统应用程序。

  1. Please check the link below on applications being signed with the platform certificate https://groups.google.com/d/msg/android-developers/wsRr-VhzKZ4/dAUbLPszOQ8J
  2. http://developer.android.com/guide/topics/security/security.html
  1. 请查看以下有关使用平台证书签名的应用程序的链接https://groups.google.com/d/msg/android-developers/wsRr-VhzKZ4/dAUbLPszOQ8J
  2. http://developer.android.com/guide/topics/security/security.html

回答by SimleGate

It can work:

它可以工作:

 String cmd = "su -c shutdown";
         try{
         Runtime.getRuntime().exec(cmd);
   }catch(IOException e){
       e.printStackTrace();
   }
         }

OtherWise,you must add android:sharedUserId="android.uid.system"into your application

否则,您必须添加android:sharedUserId="android.uid.system"到您的应用程序中

回答by android developer

If you have root, you can use any of those commands:

如果您有 root,则可以使用以下任何命令:

"hard root" :

“硬根”:

reboot

"soft reboot" :

“软重启”:

setprop ctl.restart surfaceflinger; setprop ctl.restart zygote