Android关机重启实现

Android通常是在长按Power键后出现弹出菜单,然后可以选择Power Off进行关机操作。代码实现在

frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java(mPowerLongPressRunnable)

frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java

下面是个示例程序实现关机重启和恢复出厂设置,在KK上测试通过

1. Android.mk

LOCAL_PACKAGE_NAME := PowerOff

LOCAL_CERTIFICATE := platform

2. AndroidManifest.xml

package="com.***.poweroff"

android:sharedUserId="android.uid.system"

<uses-permission android:name="android.permission.SHUTDOWN"/>

<uses-permission android:name="android.permission.MASTER_CLEAR" />

<application

android:icon="@drawable/icon"

android:label="@string/app_name" >

<activity

android:name=".PowerOff"

android:label="@string/app_name" >

<intent-filter>

<action

android:name="android.intent.action.MAIN" />

<category

android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

3. PowerOff.java

ok_poweroff.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Log.d(TAG, "Power off starting");

Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);

intent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

});

ok_restart.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Log.d(TAG, "Restarting");

Intent intent = new Intent(Intent.ACTION_REBOOT);

intent.putExtra("nowait", 1);

intent.putExtra("interval", 1);

intent.putExtra("window", 0);

sendBroadcast(intent);

}

});

ok_factoryreset.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Log.d(TAG, "Factory reset starting");

sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));

}

});

更多相关文章

  1. GridView示例2(自动增长)
  2. 通过xml加载菜单Menus
  3. Android GridView 使用示例
  4. Android Studio 官方示例源码地址
  5. android 权限示例
  6. 搞定 Android 布局2:SlidingPaneLayout 侧滑菜单面板布局
  7. Android简明开发教程二十四篇及示例代码下载
  8. android 4.2修改设置菜单的背景颜色
  9. Android菜单详解

随机推荐

  1. Python 用hashlib求中文字符串的MD5值
  2. 长安铃木经销商爬取(解析xml、post提交、p
  3. Python基础(4) - 变量
  4. 关于Python的super用法研究
  5. 以DAG方式调度作业
  6. Python阻止复制对象作为参考
  7. Python下numpy不成功的解决办法(wheel方法
  8. python应用之socket编程
  9. [Python]—Linux Server 系统监控程序
  10. 对照java和spring理解python中单例模式的