Download apk to local, error analysis occurred while installing the parsing package encountered

Source: Internet
Author: User

Encounter some problems, the result of using code is unable to meet their own needs, many times it is related to the mobile phone itself, some of the permissions and mechanisms.


Issue 1: Download apk to internal storage, unable to find the path during installation, prompt: Error parsing package

This is because of the permissions of the reason, the first internal storage, for the reason of the permissions, the default is private, internal storage so can only be called by the application itself. The installation of the application is Packagesinstaller and the other part of the Packagemanager system call, is equal to the external application call, so it is not successful, because my need is to put the apk into the internal storage of the fixed directory.

First, there are two methods for calling internal storage that can set permissions (refer to Connection: http://aijiawang-126-com.iteye.com/blog/792931)

A openfileinput (String name,int mode), the first parameter is the file name, not with "/", meaning that it cannot be a path, must be a file name, the second parameter is a pattern.

There is also the Getdir () method Getdir (String name, int mode), which returns the folder file object under/data/data/youpackagename/under the specified name, and creates a new folder with the specified name if the folder does not exist.

It seems to be possible to put this under file.

Then it is using Android.app.ContextImpl.setFilePermissionsFromMode (String name,int mode,int extrapermissions);

There is android.os.FileUtils.setPermissions (String path, int mode, int uid, int gid);

This two class is a system internal class, written in Eclipse, is not normally called, because you can not find the package, the most convenient way is to take advantage of reflection.

Probably send an example to use the reflection in a simple total, and the meaning of the parameter.

public void Setfilepathpermissions (String name, int mode,
			int extrapermissions) {
		class<?> clazz;
		try {
			//Baunianga class name
			clazz = Class.forName ("Android.app.ContextImpl");
			Methods Name Method
			= Clazz.getdeclaredmethod (
					"Setfilepermissionsfrommode", new class[] {string.class,
							Int.class, Int.class});
			Method.invoke (null, name, mode, extrapermissions);
		} catch (ClassNotFoundException e) {
			e.printstacktrace ();
		} catch (Nosuchmethodexception e) {
			E.printstacktrace ();
		} catch (Illegalaccessexception e) {
			e.printstacktrace ();
		} catch (IllegalArgumentException e) {
			E.printstacktrace ();
		} catch (InvocationTargetException e) {
			e.printstacktrace ();
		}
	}
The first parameter in Class.forName is the full name of the class plus the package name.

The first parameter in the Getdeclaredmethod method is the method name, the second parameter, which is an array of parameters, such as the Setfilepermissionsfrommode method has three parameters, followed by appending three parameters, you can write as above the number of groups, the middle of three elements ,

Clazz.getdeclaredmethod ("Setfilepermissionsfrommode", new class[] {string.class,int.class, int.class});
You can also remove the array display, as follows

Clazz.getdeclaredmethod ("Setfilepermissionsfrommode", String.class,int.class, Int.class);
There are several parameters to the method to be reflected, just write a few, what type is the method of calling type. Class. Remember, int and integer are not the same.

Method.invoke (null, name, mode, extrapermissions), which is also a two parameter pattern, if the method is static, the first argument is set to NULL, otherwise written as clazz.newinstance () ,

The following three parameters, themselves and the above type. Class is a meaning, is an array, this is the way to write directly, the three parameters passed in the OK.

If the method does not return a value, the return value type of the method name you create yourself will be written in void.


Also have their own test with the simulator, you can use the ADB to open the application under the package name permissions, must be a first-level directory open. becomes the default for externally readable and writable.

Here's how to set up the process with ADB:

Abd Root

ADB remount (Success will show remount success)

ADB shell (will become root@android:/#)

CD Data/data

LS (View list)

CD directory (enter the package name and go to each folder)

Ls-la (The following file will be viewed, the permission information will also be displayed)

Cd.. (fallback, like git)

Chmod-r 777 (directory to give permission)

Ls-la (View will find the permissions changed)

Ctrl +c (EXIT)


Actually downloading the apk to an external path can be called installation.

And me. To meet the requirements, the method is to download the file to a local custom directory, then copy to the external path, and then call the installation from the external path, and then delete it.


Today I learned a singleton mode, and now I understand.

Want to keep a value, and then each time you start, you will create a new object, and then call the default value, and finally can not meet the requirements.

public class Contance {public
	Boolean flag = true;
	private static contance minstance = null;

	public static Contance getinstance () {
		if (minstance = = null) {
			minstance = new Contance ();
			LOG.I ("Test_pass", "new instance");
		LOG.I ("Test_pass", "minstance");
		return minstance;
	}
}
This is an example, the so-called Singleton is a purpose, so-called single, is only one time, is only a new object,

When I call flag, not every time the default is true, I want to change the state, and keep this value, so I use the singleton mode.

This is how I write Contance.getinstance (). Flag;

When calling GetInstance, an instance is passed in to determine if the instance is null, if it is empty, it is not created, and it is new at this time. If it is not empty, the description is new, then return the current instance, so that only new is needed, which is the singleton pattern.


There is also Settings.system.putInt (), which cannot be called at receiver, because the process is not a system process, so the system's write method cannot be called, but Geiint is callable.

Android:shareduserid= "Android.uid.system"


There is also the ADB command, which is used to apply the restart effect. Set up the developer mode on your phone to start the ADB debug,

The ADB then executes the following command:

ADB root

Abd remount

ADB Push application Path/system/app

ADB reboot (restart)









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.