Chinaunix首页 | 论坛 | 博客
  • 博客访问: 489043
  • 博文数量: 137
  • 博客积分: 3874
  • 博客等级: 中校
  • 技术积分: 1475
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-05 10:50
文章分类

全部博文(137)

文章存档

2011年(37)

2010年(100)

分类: LINUX

2010-08-10 22:36:22

刚才想怎么把手机重启实现了,结果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
}


参考:

阅读(11764) | 评论(0) | 转发(1) |
0

上一篇:android service 介绍

下一篇:linux下格式化U盘

给主人留下些什么吧!~~