重启Android手机代码


      重启Android手机代码
作者:[tcconan] 来源:[13COM.NET收集整理] 浏览:[27] 评论:[0]
关键字:
package com.test.reboot;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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();
   }
}
}
本文来自 13COM.NET, 本文地址:http://www.13com.net/article/article.asp?articleid=1103

 

  android的重启(大部分是转载) 收藏
      前段时间有了个android的新项目,在做的过程中碰到一个问题,就是重启设备。

      找了很多资料一直都不能正确的重启设备,直到看到这篇文章:http://topic.csdn.net/u/20100423/15/34a5ca3d-df13-4056-8e62-55f6c4c5e412.html

      结合这篇文章我是这样做到重启的:

1.在AndroidManifest.xml中添加权限:android:sharedUserId="android.uid.system"。

2.在调用的地方如下:(和文章中提到的是一样的,下面还会有提到文章中讲的其它方法)

view plaincopy to clipboardprint?
Log.d(tag, "reboot");  
Intent reboot = new Intent(Intent.ACTION_REBOOT);  
reboot.putExtra("nowait", 1);  
reboot.putExtra("interval", 1);  
reboot.putExtra("window", 0);  
sendBroadcast(reboot); 
Log.d(tag, "reboot");
Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot);

      在文章中有提到很多的方法其中之一wiki14写到:(这方法我没有试,有兴趣的可以去尝试下,也告诉我结果:))

view plaincopy to clipboardprint?
//重启代码位于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  

//重启代码位于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
}
 

上面那段代码记得编译时需在Android.mk中添加这条语句:
LOCAL_CERTIFICATE := platform

      文章中的另一种方法是gz_boy写到的:

view plaincopy to clipboardprint?
//关机:  
//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);


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fjhyy/archive/2010/06/03/5644296.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值