有些Android版本没有系统重启的功能,非常不方便。需要我们自己开发一个能够重新启动的应用。

首先定义布局文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.hzhi.restart"    android:versionCode="1"    android:versionName="1.0"     android:installLocation="preferExternal"    android:sharedUserId="android.uid.system">    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.hzhi.restart.MainActivity"            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></manifest>

布局文件其实很简单,就是一个按钮。注意android:sharedUserId="android.uid.system",这是为了让应用分享一个系统级别的UID,否则会出现权限拒绝的错误。

类文件:

package com.hzhi.restart;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void click(View view){Intent intent = new Intent();intent.setAction(Intent.ACTION_REBOOT);intent.putExtra("nowait", 1);intent.putExtra("interval", 1);intent.putExtra("startTime", 1);intent.putExtra("window", 0);sendBroadcast(intent);}}

运行后会出错,这是因为程序运行时,使用的是系统默认的签名,而不是系统级别的签名。解决方法是将默认的签名删除,替换成系统级别的签名(此过程太繁琐,不再详细叙述)。

更多相关文章

  1. SD卡读写文件 代码学习
  2. Android B/S 关于系统兼容性相关问题
  3. 安装|卸载apk文件在Android仿真器中
  4. Android XML文件 Error parsing XML: unbound prefix...
  5. Android 编译系统理解:envsetup.sh学习
  6. Android10共享文件总是读取不到文件,文件资源不存在!
  7. android 输入系统
  8. 关于华为P10(Android 8.0系统)出现的一个莫名奇妙的ANR
  9. Android学习之文件存储

随机推荐

  1. Android(安卓)开发技术周报 Issue#281
  2. Android(安卓)ViewPager+TabHost实现首页
  3. Android布局管理器-详细解析布局实现
  4. gravity和layout_gravity
  5. 22个值得收藏的android开源代码-UI篇
  6. Android异步任务AsyncTask实现方式
  7. Android——Tomcat+MySQL+Servlet,实现将C
  8. Android异步消息处理机制 全解析
  9. 我的第一个Android软件——简单拨号器
  10. 开源项目之Android框架(Query)