简体   繁体   English

在android中获取关闭或重启的权限

[英]Obtain the permission of shutdown or restart in android

I am creating a shutdown/restart function. 我正在创建一个关机/重启功能。

    Intent intent = new Intent();  
    intent.setAction("android.intent.action.ACTION_SHUTDOWN");  
    sendBroadcast(intent); 

In xml: 在xml中:

<uses-permission  android:name="android.permission.SHUTDOWN"/>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="org.crazyit.net"
  android:versionCode="1"
  android:versionName="1.0" 
  android:sharedUserId="android.uid.system">

But it has this error in users-permission: permission is only granted to system apps . 但它在users-permission中有此错误: permission is only granted to system apps

Any idea? 任何想法?

As the error message explains, a normal app cannot get this permission. 正如错误消息所解释的那样,普通应用无法获得此权限。 It would be a security risk since you could build an application that renders the phone useless. 这将是一个安全风险,因为您可以构建一个使手机无用的应用程序。

Normal SDK applications cannot cause phones to shut down or reboot. 普通的SDK应用程序无法导致手机关机或重启。 Only applications that are part of the firmware (ie, signed by the firmware sining key) can hold the proper permission and perform those actions. 只有属于固件的应用程序(即由固件主题密钥签名)才能保留适当的权限并执行这些操作。

In android SDK documents, It is clearly stated that the ACTION_SHUTDOWN and ACTION_REBOOT are protected intents that can only be sent by the system". You don't have the privilege to use this intent to reboot the device for security reason. 在android SDK文档中,明确指出ACTION_SHUTDOWNACTION_REBOOT是受保护的意图,只能由系统发送“。出于安全原因,您无权使用此意图重启设备。

In my case i want to launch my app on mobile switch on and set its status on mobile switched off, So for that i use in this way 在我的情况下,我想在移动开关上启动我的应用程序并在移动设备关闭时设置其状态,所以为此我用这种方式

 <receiver android:name="WifiNotification" android:enabled="true" android:permission="android.permission.CHANGE_WIFI_STATE">
        <intent-filter >
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            <action android:name="android.net.wifi.SCAN_RESULTS"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
        <intent-filter >
            <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
        </intent-filter>
    </receiver> 

and in my WifiNotification i did :: 在我的WifiNotification中我做了::

public void onReceive(Context context, Intent intent) {

   if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)
   {.
    .
    .
    }
    else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_SHUTDOWN))
    {.
    .
    .
    }
}

One of the permission is :: 其中一个许可是::

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

If your device is rooted, you can use this code for shutdown 如果您的设备已植根,则可以使用此代码进行关闭

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot -p" });

and use this code for Reboot 并使用此代码重新启动

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });

I use this on Xamarin.Android 我在Xamarin.Android上使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码以重新启动,关机并进入Android手机的下载模式? - Code to Restart , Shutdown and enter in to Download Mode for Android Phones? Android Pie (9.0) - 写入关机或重启 Function - 应用程序是特权 - Android Pie (9.0) - Writing a Shutdown or Restart Function - Application is Privileged android api 关机和重启后自动启动活动 - android api auto startup activity after shutdown and restart 为Android保留UPnP库。 关闭并重新启动后看不到设备 - Cling UPnP library for Android. Not seeing devices after shutdown and restart Android 9 应用程序或 MDM 关机和重启、超时屏幕或睡眠 - Android 9 App or MDM shutdown and restart, timeout screen or sleep 如何在有 root 权限的手机上使用 android.permission.SHUTDOWN? - How can I use android.permission.SHUTDOWN on a rooted phone? Android应用程序能否获得对/ mnt / asec或/ mnt / obb的写许可? - Can an Android app obtain write permission to /mnt/asec or /mnt/obb? 在运行时授予用户权限后,Android 6.0需要重新启动 - Android 6.0 needs restart after granting user permission at runtime 手动更改权限后如何在android棉花糖中重新启动应用 - How to Restart the app in android marshmallow when permission changed manually 在 Android 应用程序中重新启动后尝试打开文件夹时出现权限错误 - Permission error when trying to open folder after restart in Android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM