Android program update (no sdcard)

Source: Internet
Author: User

 

Summary: The program we wrote needs to be updated (the old version has been installed by default), but there is no sdcard when the user updates it. If sdcard exists, so you don't need to talk nonsense...

 

 

 

First, let's talk about the basic principles of software updates. Download the apk of a program from the server and start the installation. Generally, we use sdcard operations, for example, you can do some image caching or record some user information. Today, we want to summarize the practices when the user does not have sdcard.

 

 

 

1. The first problem we have before us is that without sdcard, where should we download the apk file? Where can I download it? This is the most important question!

 

Android is a linux kernel system, so android complies with linux specifications, such as file permissions.

 

Where can the apk file be stored first?

 

The answer is the directory where your application is located.

 

Maybe some people do not know which directory the application is located. It is very simple. They only need to go to the console (or in the DDMS provided by eclipse, of course, not all models of DDMS can be seen) enter the/data/directory, and then enter the com. xxx is the program directory installed on your mobile phone! Put a thumbnail...

 

The package name of my project is onerain. ha. So, the directory I just mentioned is/data/onerain. ha! (This is the linux directory, not the. suffix file in windows ).

 

PS: If your machine is licensed, you cannot use the ls command without obtaining the root permission. You just cannot see the file information contained in the directory more clearly, but it will not affect your operations!

2. How to get this directory?

 

Three methods are used here, so three directories are generated. The Code is as follows:

<Span style = "font-size: 18px;"> /**

* The first method can be put in a cache directory provided by the android program.

*/

File cacheDir = getCacheDir ();

System. out. println (cacheDir. getPath ());

/**

* In the second method, we can create a directory by ourselves,

*/

File dir = getDir ("aaa", Context. MODE_PRIVATE | Context. MODE_WORLD_READABLE | Context. MODE_WORLD_WRITEABLE );

System. out. println (dir. getPath ());

 

System. out. println (getPackageName ());

Try

{

/**

* In chapter 3, directly create a file, which will be placed under/data/onerain. ha/files /.

*/

FileOutputStream fos = openFileOutput ("test ",

Context. MODE_PRIVATE | Context. MODE_WORLD_WRITEABLE | Context. MODE_WORLD_READABLE );

}

Catch (FileNotFoundException e)

{

// TODO Auto-generated catch block

E. printStackTrace ();

} </Span>

 

Then your project directory will become like this.

For personal comparison, the second method is recommended, because permission, in which only app_aaa is rwx permission for other users (because we want to write content in the directory, that is to say, after downloading data from the server, the w permission is required. To enter this directory, the x permission is required )! Of course, if you just don't want to write in this directory, it doesn't matter. The permissions can be modified!

3. Modify permissions

 

This should be a linux issue. Of course it is also very simple. The command line is like this.

 

Chmod [command] [file/directory]

 

In a brief introduction to commands, there are three types of users in linux. The letter u represents the owner (user), g Represents the owner's group (group), and o Represents other users (other ), a Indicates all, r indicates readable, w indicates writable, and x indicates executable.

 

If you want to change a file to readable and executable for all users, you can write commands like this.

 

Chmod a + rwx/data/oneran. ha/cache can also be written as chmod ugo + rwx/data/onerain. ha/cache

 

Of course, there is also a way to use numbers, the above command can also be written like this

 

Chmod 777/data/onerain. ha/cache

 

The three digits correspond to the owner, the owner's group, and other users, while the rwx value is 421. If it is 7, it indicates that it is 4 + 2 + 1, that is, full permission!

 

PS: It's not far away. Here we will introduce how to modify permissions not only to modify this directory, but also to modify the permissions of the apk file you downloaded, it is not executable by default after the download !!!

4. Modify permissions in the program

 

The permission will be modified in the command line, but what we want is to execute these commands in the program! So how?

 

The downloaded apk file has insufficient permissions. You can see the code + diagram.

<Span style = "font-size: 18px;"> File apkFile = new File (dir. getPath () + "/test.apk ");

If (! ApkFile. exists ())

{

Try {

ApkFile. createNewFile ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

} </Span>

 

You can see that only the owner has the rw permission .... Ah! So we need to add this sentence to our code.

<Span style = "font-size: 18px;"> String [] command = {"chmod", "777", dir. getPath () + "/test.apk "};

ProcessBuilder builder = new ProcessBuilder (command );

Try {

Builder. start ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

</Span>

 

Let's look at the picture again.

 

Look, our apk file permissions, haha, are successful! The rest of the work is to start an Intent to install this apk with the third-party installer of the android system, and then overwrite the previous one! Because it is a third-party, o + x is an essential component (that is, other users have executable permissions)

You can also perform this operation. It is not only possible for a root user to use a water terminal!

 

From the oneRain88 Column

Related Article

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.