1

我想在按钮单击时打开设备的屏幕,我使用的代码如下

public class MainActivity extends Activity {

Button powerOff;
int amountOfTime =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

powerOff = (Button)findViewById(R.id.button1);
powerOff.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);

        // Choice 1
        manager.goToSleep(amountOfTime);

        // Choice 2
        PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
        wl.acquire();
        wl.release();
    }
});
}

也获得了许可

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

它仍然显示一个例外

   01-14 16:06:00.875: E/AndroidRuntime(9317): FATAL EXCEPTION: main
   01-14 16:06:00.875: E/AndroidRuntime(9317): java.lang.SecurityException: Neither user 10079  nor current process has android.permission.DEVICE_POWER.
   01-14 16:06:00.875: E/AndroidRuntime(9317):  at android.os.Parcel.readException(Parcel.java:1322)
   01-14 16:06:00.875: E/AndroidRuntime(9317):  at android.os.Parcel.readException(Parcel.java:1276)
  01-14 16:06:00.875: E/AndroidRuntime(9317):   at android.os.IPowerManager$Stub$Proxy.goToSleep(IPowerManager.java:341)
  01-14 16:06:00.875: E/AndroidRuntime(9317):   at android.os.PowerManager.goToSleep(PowerManager.java:464)
4

2 回答 2

2

您缺少DEVICE_POWER权限。

编辑:显然,只允许系统应用程序使用此权限,所以你实际上被卡住了。

于 2013-01-14T10:48:28.013 回答
0

向您的清单添加以下权限:

<uses-permission android:name="android.permission.DEVICE_POWER"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
于 2013-01-14T10:54:26.930 回答