gpt4 book ai didi

android - 通过代码/程序关闭或重启 Android 设备?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:40 27 4
gpt4 key购买 nike

有什么方法可以直接从应用程序重启/关闭手机吗?

例如:当我遇到某些特定情况时,我需要重新启动/关闭我的手机...

对开发者网站的引用:

允许重启?

http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

允许变砖设备???

http://developer.android.com/reference/android/Manifest.permission.html#BRICK

重启方法???

http://developer.android.com/reference/android/os/PowerManager.html#reboot%28java.lang.String%29

重启和删除的方法??

http://developer.android.com/reference/android/os/RecoverySystem.html#rebootWipeUserData%28android.content.Context%29

MonkeyRunner/MonkeyDevice 中的重启方法:

http://developer.android.com/guide/developing/tools/MonkeyDevice.html#reboot

有一些选项可以使设备变砖,但为什么不关闭或重启呢?

我尝试了下面的代码,但它抛出了一个异常..

list .xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.schogini.PowerOff"
android:versionCode="1"
android:versionName="1.0">


<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.DEVICE_POWER" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PowerOffActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

Java 代码:

package com.schogini.PowerOff;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;


public class PowerOffActivity extends Activity {

PowerManager pm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Button mBuyButton = (Button) findViewById(R.id.button1);

mBuyButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
pm.reboot("null");
}
});
}
}

LogCat 抛出异常:

06-10 17:58:29.001: WARN/dalvikvm(2064): threadid=3: thread exiting with uncaught exception (group=0x4001b160)
06-10 17:58:29.001: ERROR/AndroidRuntime(2064): Uncaught handler: thread main exiting due to uncaught exception
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): java.lang.NoSuchMethodError: android.os.PowerManager.reboot
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.schogini.PowerOff.PowerOffActivity$1.onClick(PowerOffActivity.java:28)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.View.performClick(View.java:2364)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.View.onTouchEvent(View.java:4179)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.widget.TextView.onTouchEvent(TextView.java:6545)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.View.dispatchTouchEvent(View.java:3709)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.os.Looper.loop(Looper.java:123)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at java.lang.reflect.Method.invoke(Method.java:521)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-10 17:58:29.011: ERROR/AndroidRuntime(2064): at dalvik.system.NativeStart.main(Native Method)

最佳答案

不是标准的 Android 发行版,不是。获得适当 root 权限的手机通常可以访问 su/reboot 命令,但对于现成的商用设备,不行:没有办法做到这一点。

关于android - 通过代码/程序关闭或重启 Android 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6305491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com