原文地址:http://my.oschina.net/blackylin/blog/71692
1.在AndroidManifest.xml文件的manifest标签中加入一条android:sharedUserId="android.uid.system"
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ipanel.update"
- android:versionCode="1"
- android:versionName="1.0"
- android:sharedUserId="android.uid.system" >
- <uses-sdk
- android:minSdkVersion="9"
- android:targetSdkVersion="15" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/title_activity_main" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ipanel.update"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.在MainActivity中,有以下2种方式实现:
- /*Intent reboot = new Intent(Intent.ACTION_REBOOT);
- reboot.putExtra("nowait", 1);
- reboot.putExtra("interval", 1);
- reboot.putExtra("window", 0);
- sendBroadcast(reboot); */
- PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
- pManager.reboot("");
/*Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot); */
PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("");
整个代码:
- package com.demo.reboot;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.PowerManager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button rebootBtn = (Button) findViewById(R.id.button2);
- rebootBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("提示")
- .setMessage("确认重启么?")
- .setPositiveButton("重启", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 重启
- /*String str = "重启";
- try {
- str = runCmd("reboot", "/system/bin");
- } catch (IOException e) {
- e.printStackTrace();
- }*/
- /*Intent reboot = new Intent(Intent.ACTION_REBOOT);
- reboot.putExtra("nowait", 1);
- reboot.putExtra("interval", 1);
- reboot.putExtra("window", 0);
- sendBroadcast(reboot); */
- PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
- pManager.reboot("重启");
- System.out.println("execute cmd--> reboot\n" + "重启");
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 取消当前对话框
- dialog.cancel();
- }
- }).show();
- }
- });
- }
- }
package com.demo.reboot;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rebootBtn = (Button) findViewById(R.id.button2);
rebootBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage("确认重启么?")
.setPositiveButton("重启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 重启
/*String str = "重启";
try {
str = runCmd("reboot", "/system/bin");
} catch (IOException e) {
e.printStackTrace();
}*/
/*Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot); */
PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("重启");
System.out.println("execute cmd--> reboot\n" + "重启");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 取消当前对话框
dialog.cancel();
}
}).show();
}
});
}
}
3.给apk签名
签名方法:
1)添加权限
在AndroidManifest.xml文件下添加android:sharedUserId="android.uid.system" 。
2)在Eclipse中导出无签名的应用文件
在工程中:右键->Android Tools -> Export Unsigned Application Package导出应用
3)找出系统签名密钥
系统密钥为:platform.pk8和platform.x509.pem
路径: build\target\product\security
4)找出系统签名工具
工具为:signApk.jar
路径:/out/host/linux-x86/framework/ signApk.jar
5)开始签名
将第2、3、4步找到的无签名应用、platform.pk8、platform.x509.pem和signApk.jar放到同一文件夹下如F:\sign。
打开 dos 操作界面,定们到F:\sign,输入命令:
java -jar signapk.jar platform.x509.pem platform.pk8 a.apk b.apk
(a.apk 为未签名应用 b.apk 为签名之后应用)