1. Add an Android :sharedUserId="android.uid.system" to the manifest tag of the AndroidManifest.xml file
<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. In MainActivity, there are two ways to implement:
/*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("");
whole code:
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("Title") .setMessage("Are you sure to restart?") .setPositiveButton("重启", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // restart /*String str = "Restart"; 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("reboot"); System.out.println("execute cmd--> reboot\n" + "重启"); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Cancel the current dialog dialog.cancel(); } }).show(); } }); } }
3. Sign the apk
Signature method:
1) Add permission
Add android:sharedUserId="android.uid.system" under the AndroidManifest.xml file.
2) Export the unsigned application file in Eclipse in
the project: right click -> Android Tools -> Export Unsigned Application Package to export the application
3) Find out the system signature key The
system key is: platform.pk8 and platform.x509.pem
path: build\target\product\security
4) Find out the system signature tool
tool: signApk.jar
path: /out/host/linux-x86/framework/signApk.jar
5) Start signing
Put the unsigned application, platform.pk8, platform.x509.pem and signApk.jar found in steps 2, 3 and 4 into the same folder as F:\sign.
Open the dos operation interface, go to F:\sign, enter the command:
Java -jar signapk.jar platform.x509.pem platform.pk8 a.apk b.apk
(a.apk is the unsigned application and b.apk is the signed one application)