Android编程实现系统重启与关机的方法

2019-12-10 19:00:31王冬梅

具体含义可自行查阅资料。注意:

LOCAL_CERTIFICATE := platform

3. 编写补充 AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://www.easck.com/apk/res/android"
  package="mark.zhang"
  android:versionCode="1"
  android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:sharedUserId="android.uid.system" >
<activity
      android:label="@string/app_name"
      android:name=".RebootActivity" >
      <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
</application>
</manifest>

注意:

android:sharedUserId="android.uid.system"

4. 编写逻辑代码 RebootActivity.java

package mark.zhang;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class RebootActivity extends Activity {
  // 是否显示关机确认的对话框
  // false 不显示确认关机的对话框,直接关机
  // true 显示确认关机的对话框,让用户选择是否确认关机
  public static final boolean showShutdownDialog = false;
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
/**
   * 发送广播.
   *
   * @param view
   */
  public void onReboot(View view)
  {
    Intent reboot = new Intent(Intent.ACTION_REBOOT);
    reboot.putExtra("nowait", 1);
    reboot.putExtra("interval", 1);
    reboot.putExtra("window", 0);
    sendBroadcast(reboot);
  }
/**
   * 启动 Activity.
   *
   * @param view
   */
  public void onShutdown(View view)
  {
    public void onShutdown(View view)
   {
    Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
    shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, showShutdownDialog);
    shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(shutdown);
   }
  }
}