刚才想怎么把手机重启实现了,结果google以android reboot搜索,竟然没有结果,可能是关键字有问题。只好换百度,还好,有答案了。以下是几种解决方案:
第一种是调用shell里面的reboot命令。
public class RebootAndroid extends Activity implements OnClickListener {
private Button btnReboot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnReboot = (Button) findViewById(R.id.btnReboot);
btnReboot.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String cmd = "su -c reboot";
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
new AlertDialog.Builder(this).setTitle("Error").setMessage(
e.getMessage()).setPositiveButton("OK", null).show();
}
}
}
|
还有一种是调用系统提供的api
关机:
In frameworks/base/services/java/com/android/server/BatteryService.java
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
重启:
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
|
还有一种,没有测试的:
//重启代码位于frameworks\base\core\jni\android_os_Power.cpp,里面有
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{/*关机*/
sync();
#ifdef HAVE_ANDROID_OS
reboot(RB_POWER_OFF);
#endif
}
static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
{/*重启*/
sync();
#ifdef HAVE_ANDROID_OS
if (reason == NULL) {
reboot(RB_AUTOBOOT);
} else {
const char *chars = env->GetStringUTFChars(reason, NULL);
__reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, (char*) chars);
env->ReleaseStringUTFChars(reason, chars); // In case it fails.
}
jniThrowIOException(env, errno);
#endif
}
|
参考:
阅读(11837) | 评论(0) | 转发(1) |