[Reprint] How to make Android app get system permissions to modify system time

Source: Internet
Author: User

The Systemclock.setcurrenttimemillis () function is available in the Android API to modify the system time, but no matter how you call this function is useless, regardless of the emulator or the real machine, in Logcat always get "unable to Open alarm driver:permission denied ". This function needs to be rooted or run with the system process.

Originally thought there is no way in the application of this layer to change the system time, and later on the internet search for a long, know that the purpose can still be achieved.

The first method is simple, but it needs to be compiled with make in the context of the Android system source code:

1. Add the android:shareduserid= "Android.uid.system" attribute to the manifest node in the application's androidmanifest.xml.

2. Modify the Android.mk file, add local_certificate: = Platform this line

3. Using the MM command to compile, the generated apk will have the ability to modify the system time.

The second method is troublesome, but do not need to open the virtual machine to the source environment with make to compile:

1. Ibid., add the android:shareduserid= "Android.uid.system" attribute.

2. Use eclipse to compile the apk file, but this apk file is not available.

3. Open the APK file with the compression software, delete the cert.sf and cert.rsa two files in the Meta-inf directory.

4. Use the target system's platform key to re-sign the apk file. This step is more troublesome, first find the key file, the location in my Android source directory is "Build/target/product/security", below the Platform.pk8 and Platform.x509.pem two files. Then use Android to provide the signapk tool to sign, signapk source code is under "build/tools/signapk", the use of "signapk Platform.x509.pem platform.pk8 input.apk output.apk ", the file name is best to use absolute path to prevent not found, you can also modify the source code to use directly.

This way the last apk is the same as the first one.

Finally explain the principle, first add android:shareduserid= "Android.uid.system" this attribute. With the shared user ID, multiple apk with the same user ID can be configured to run in the same process. Then the UID of the program into a android.uid.system, that is, to let the program run in the system process, so that there is permission to modify the system time.

Just adding UID is not enough, if you install the APK at this time found unable to install, prompt signature does not match, because the program wants to run in the system process and the target system platform key, The second method mentioned above is the PLATFORM.PK8 and Platform.x509.pem two files. Using these two keys to sign the APK before it can actually be put into the system process. The first method adds Local_certificate: = Platform is actually signed with these two keys.

There is also a problem, that is, the generated programs can only be used in the original Android system or in their own compiled system, because such a system can get platform.pk8 and Platform.x509.pem two files. If the other company does not have the Android installed on the installation. Try the original Android key to sign, the program runs OK on the emulator, but put on the G3 to install the direct prompt "package ... has no signatures that match those in shared user Android.uid. System ", which also protects the security of the systems.

I use the second method, the first solution is not clear, really difficult to understand, the second solution is not clear, I tried many times to succeed, and now the specific implementation steps to share to you:

Note: The fourth step after the work in the source of the environment to operate, my environment is Ubuntu + source, the source of the compiled image should be the image of your target.

1, first in the manifest file added android:shareduserid= "Android.uid.system" this property.

2. Compile the xxx.apk file with Eclipse, but this apk file is not available.

3. Use the compression software to open the xxx.apk file, delete the Meta-inf directory cert.sf and Cert.rsa two files, Win32 system with WinRAR on it.

4. Use the target system's platform key to re-sign the apk file. This step is more troublesome, first find the key file, in my Android source directory location is "Build/target/product/security", The following platform.pk8 and Platform.x509.pem two files, you can copy these two files to the TMP directory, the third step to delete the file after the APK package is also copied to this directory.

5, compiled Signapk.jar files, signapk source code in the Android source directory/build/tools/signapk, the first CD to change the directory, and then through the Javac The Signapk.java command compiles the original file, gets the Signapk$signatureoutputstream.class and Signapk.class files, and then manually creates the k/com/android/signapk, noting that This directory must be under the source build/tools/signapk directory, the PWD display directory should be/build/tools/signapkk/com/android/signapk, and then put two class files in the directory, and then in the/ The build/tools/signapk directory executes the jar CVFM Signapk.jar signaapk.mf-c k/. This allows you to generate a Signapk.jar file in this directory, get the signature program provided by Android, and have the privileges of the system. (If there is an exception, it means you have a problem with the operation, it is not correct)

6. Copy the generated Signapk.jar file to the TMP directory of the source code, then use the command: Java-jar signapk.jar platform.x509.pem platform.pk8 xxx.apk xxx_new.apk, If there is no exception, the newly generated xxx_new.apk has been signed.

7. Load the generated apk into the target machine to run, such as adb install xxx_new.apk.

Then take a look at how to modify the system time! In the source code is this:

1 Static voidSetDate (intYearintMonthintDay ) {2Calendar C =calendar.getinstance ();3 4 C.set (calendar.year, year);5 C.set (Calendar.month, MONTH);6 C.set (Calendar.day_of_month, day);7         Longwhen =C.gettimeinmillis ();8 9 if(When/1000 <integer.max_value) {Ten Systemclock.setcurrenttimemillis (when); One         } A     } -  - Static voidSetTime (intHourofday,intminute) { theCalendar C =calendar.getinstance (); -  - C.set (Calendar.hour_of_day, hourofday); - C.set (Calendar.minute, MINUTE); +C.set (Calendar.second, 0); -C.set (Calendar.millisecond, 0); +         Longwhen =C.gettimeinmillis (); A  at if(When/1000 <integer.max_value) { - Systemclock.setcurrenttimemillis (when); -         } -}

Attached Source: http://download.csdn.net/detail/weijing331/4530873

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.