文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>android 手机重启实现

android 手机重启实现

时间:2010-08-10  来源:niuniu2006t

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


参考: http://blog.csdn.net/fjhyy/archive/2010/06/03/5644296.aspx http://www.hiapk.com/bbs/viewthread.php?tid=72796&page=1
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载