This is often the case in application development, how to use code in the application to restart the phone, in addition, we know that after the restart, our application can register the broadcast receiver, to ensure that our application will be the first to open our services, This way our code will be the first to receive a broadcast event that receives a text message. All right, now let's get together to implement an example of a phone restart using system permissions.
First, to achieve
Our implementation is very simple, is to give the interface a button, and then set the button click Things, in the Click event to complete the phone restart operation.
1. layout file
The layout file is simple, that is, a button is placed to set the Click event.
The specific code is as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " android:orientation=" vertical " tools:context=". Mainactivity "> <button android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:onclick=" click " android:text=" reboot system/> </LinearLayout>
2, Mainactivity
This class is the implementation of the button click event, in the Click event to realize the restart of the phone operation.
Package Com.lyz.reboot.activity;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 PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} public void Click (View v) {Intent Intent = new Intent (); intent.setaction (intent.action_reboot); Intent.putextra ("NoWait ", 1); Intent.putextra (" Interval ", 1), Intent.putextra (" window ", 0); Sendbroadcast (intent);}}
3. Authorization Document Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.lyz.reboot.activity " android:versioncode=" 1 " android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application" android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= " Com.lyz.reboot.activity.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>
Third, the Operation effect
Follow the above implementation, we start to run the program, and then click the button will report the following error message
caused By:java.lang.SecurityException:Permission Denial:not allowed to send broadcast Android.intent.action.REBOOT fro M pid=1786, uid=10033
This is becauseThere are many restrictive actions in the Android system that do not allow general applications to be called, such as restarting the phone, turning off the phone, modifying the system time, accessing restricted files, and so on.
Iv. re-implementation of 1, the solution of ideas
disguise our application as a system application2. Implementation stepsShared SystemsUIDusing System signing (matching signature requires public key and key two file generationKeyStore)
3, concrete implementation 1) Modify Androidmanifest.xml
We add the following attribute to the manifest node of the Androidmanifest.xml file to share the system UID
Android:shareduserid= "Android.uid.system"
The complete code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" com.lyz.reboot.activity "android:versioncode=" 1 "Android:versionnam E= "1.0" android:shareduserid= "Android.uid.system" > <uses-sdk android:minsdkversion= "8" Android: targetsdkversion= "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launche R "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android Oid:name= "com.lyz.reboot.activity.MainActivity" android:label= "@string/app_name" > <intent-fil ter> <action android:name= "Android.intent.action.MAIN"/> <category android:name = "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </APPLICATION&G T;</manifest>
2) build apk file with Eclipse
Select the project where you want to generate the APK file, and right-click to export the project to the specified directory (the steps I will not explain here).
3) using the Signature tool to generate the system signature APK
Here I use a tool to generate the system signature APK, the directory structure of this tool such as:
We put the exported apk file in the folder shown
Such as:
Then we open the CMD console, enter the directory shown, and enter the command in the following format:
Java-jar Signapk.jar Platform.x509.pem platform.pk8 input.apk output.apk
Where input.apk is the APK file to be signed, output.apk is the apk file for the output after signing
The operation is as follows:
The commands I enter here are:
Java-jar Signapk.jar Platform.x509.pem platform.pk8 reboot.apk new.apk
After the command executes, an APK with a system signature is generated in the current directory, and the APK file is not new.apk,
Such as:
This new,apk file is the apk file that we eventually generated.
4) Install APK file
First, we need to uninstall the original installed APK file on the phone, and then, on the command line, enter the following command to install the new,apk file
ADB install new.apk
If the new.apk file is in the directory where the command line is currently located, see the following:
If "Success" is prompted after installation, the installation is successful.
Five, the operation
Click the button to run the effect as
The above realization of Android using system permissions to achieve the function of mobile phone restart.
Six, warm tips
In this example, for the sake of the aspect, I write some text directly in the layout file and the related class, Everyone in the real project to write these words in the String.xml file, in the external reference to these resources, remember, this is the most basic development knowledge and specifications as an Android programmer, I am here just to facilitate directly written in the class and layout files.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android--using system privileges to restart the phone