The problem that the APK is downloaded to cash or the installation package under data/data/packageName fails to be parsed

To sum up today, the program we wrote needs to be updated (the default user has already installed the old version), but the user does not have an sdcard when updating. If there is an sdcard, then no nonsense. . .

 

Let's talk about the basic principle of software update first, download apk of a program from the server, and then start the installation. Usually, we use it to operate in the sdcard, such as doing some picture caching or recording some user information and so on. , and what I want to summarize today is the practice when the user does not have an sdcard.

 

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

android is the system of linux kernel, so android also conforms to linux specifications, such as file permissions

First of all, where can the apk file be placed?

The answer is the directory where your application resides.

Maybe some people don't know which directory the application is in. It's very simple, just go to /data/data/ in the console (or in the DDMS that comes with eclipse, of course, not all models can see DDMS) directory, and then enter the com.xxx and other directories displayed by the linux command ls -al, which are the program directories installed on your mobile phone! Put a small picture. . .



 

And the package name of my project is onerain.ha, so the directory I just mentioned is the /data/data/onerain.ha/ directory! (This is the directory of linux, not the file with the . suffix under windows).

PS: If your machine is licensed, you cannot use the ls command without root privileges, you just can't see the file information contained in the directory more clearly, but it will not affect your operation!

 

2. How to get this directory?

I have used three methods here, so three directories will be generated, the code is as follows

1.  /** 

2.            *  The first way can be placed in a cache directory provided by the android program for us 

3.           */  

4.          File cacheDir = getCacheDir();  

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

6.  /**         

7.            *  The second way we can create a directory by ourselves, 

8.          */  

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

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

11.   

12.         System.out.println(getPackageName());  

13.           

14. try           

15.         {  

16. /**             

17.              * 第三章方式直接创建文件,会放在/data/data/onerain.ha/files/下面 

18.              */  

19.             FileOutputStream fos = openFileOutput("test",   

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

21.     }   

22.         catch (FileNotFoundException e)   

23.         {  

24.             // TODO Auto-generated catch block  

25.             e.printStackTrace();  

26.     } 


然后你的工程目录就会变成这样,截图了


个人比较推荐第二种方式,原因是权限,上图中,只有app_aaa是对于其他用户来说rwx权限的(因为我们要在目录下写内容,即从服务器下载之后写流,所以这个w权限是必须的,而你要进入到这个目录下,x权限也是必须的)!当然,如果你就是不想在这个目录下写也没关系,权限是可以修改的!

 

3.修改权限

这个应该属于linux的问题,当然也是很简单的,命令行是这样的

chmod [指令] [文件/目录]

指令简单介绍下,linux下分为三种使用者,字母u代表拥有者(user),g代表拥有者所在的组(group),o代表其他用户(other),a代表全部,而字母r代表可读,w代表可写,x代表可执行

如果要把某个文件修改为对于所有用户可读可写可执行,可以这样写指令

chmod a+rwx/data/data/oneran.ha/cache  当然也可以写成 chmod ugo+rwx /data/data/onerain.ha/cache

当然还有一种用数字表示的方法,上述命令还可以这样写

chmod 777 /data/data/onerain.ha/cache

三位数字对应的是拥有者,拥有者所在的组,其他用户,而rwx对应的值是421,如果是7说明是4+2+1即全权限!

PS:扯远了,其实也不远,这里介绍修改权限不只是为了修改这个目录,还要修改你下载的apk文件的权限,因为下载之后默认是不可执行的!!!

 

4.程序中修改权限

在命令行中会修改权限了,可是我们想要的是在程序中执行这些命令的功能啊!那如何呢?

我们下载的apk文件是权限不够的,可以看下,代码+图

[java] view plaincopyprint?

1.  File apkFile = new File(dir.getPath() + "/test.apk");  

2.          if(!apkFile.exists())  

3.          {  

4.              try {  

5.                  apkFile.createNewFile();  

6.              } catch (IOException e) {  

7.                  // TODO Auto-generated catch block  

8.                  e.printStackTrace();  

9.              }  

10.         }


看到了吧,只有拥有者才有rw权限。。。。坑爹吧!


所以我们的代码还要加上这句:

1.   String[] command = {"chmod""777", dir.getPath() + "/test.apk"};  

2.          ProcessBuilder builder = new ProcessBuilder(command);  

3.          try {  

4.              builder.start();  

5.          } catch (IOException e) {  

6.              // TODO Auto-generated catch block  

7.              e.printStackTrace();  

8.          }  

再看图吧,有图有真相


看,我们的apk文件的权限,哈哈,成功了!剩下的工作就是启动一个Intent去用android系统自带的第三方安装程序去安装这个apk了,就会把之前的覆盖掉了!因为是第三方的,所以o+x是必不可少的撒(即其他用户有可执行的权限)



注意:如果我们在data/data/package name/files/ 目录下又建立了新目录,然后把apk文件放在该新目录下。这个时候这个新目录的权限也要修改为777,不然只改apk文件的权限也是不行的。


原文地址:http://blog.csdn.net/onerain88/article/details/7035723

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324576133&siteId=291194637