Android程序中安装APP总结

发布时间:2014-10-23 23:23:30
来源:分享查询网

http://blog.csdn.net/wocao1226/article/details/8980368 目录(?)[+] −目录     Android程序中安装APP总结         安装APP             方法1(普通)             方法2(静默)             方法3         卸载APP             方法1             方法2         打开APP         下载APP Android程序中安装APP总结 安装APP 方法1(普通) 这种方法通过 Intent 机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据。 String fileName = Environment.getExternalStorageDirectory() + apkName ; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); startActivity(intent); 方法2(静默) 这种方法     需要对apk进行系统签名     需要系统权限,在manifest头部加上 android:sharedUserId=“android.uid.system”     需要安装权限 <uses-permission android:name=“android.permission.INSTALL_PACKAGES” />     稍微修改下命令就可以用于静默卸载     可以把apk push到/system/app目录,也可以直接安装,两种方式都有效 如果这个apk是系统应用( /system/app/ ),那么对它使用这种方式进行更新版本时,会在 /data/app/ 下面安装apk包,更新后的应用可以直接卸载,一旦卸载,在桌面上显示的就是原来的系统应用( /system/app/ )。对这个系统应用进行多次更新,更新后均会在/data/app目录下,而之前的包会被替换掉,在这个目录只会有一个该应用的包。 private String excuteCommand() {         String result = "";         ProcessBuilder processBuilder = new ProcessBuilder(args);         Process process = null;         InputStream errIs = null;         InputStream inIs = null;         try {             ByteArrayOutputStream baos = new ByteArrayOutputStream();             int read = -1;             process = processBuilder.start();             errIs = process.getErrorStream();             while ((read = errIs.read()) != -1) {                 baos.write(read);             }             baos.write('\n');             inIs = process.getInputStream();             while ((read = inIs.read()) != -1) {                 baos.write(read);             }             byte[] data = baos.toByteArray();             result = new String(data);         } catch (IOException e) {             e.printStackTrace();         } catch (Exception e) {             e.printStackTrace();         } finally {             try {                 if (errIs != null) {                     errIs.close();                 }                 if (inIs != null) {                     inIs.close();                 }             } catch (IOException e) {                 e.printStackTrace();             }             if (process != null) {                 process.destroy();             }         }         return result;     } 代码执行后,如果安装成功的话获取到的result值是“pkg: /data/local/tmp/test.apk /nSuccess”,如果是失败的话,则没有结尾的“Success”。  public static boolean installSilence(String apkPath) {         String[] args = { "pm", "install", "-r", apkPath };         String result = excuteCommand(args);         if (StringUtil.isEmpty(result)) {             return false;         } else {             return result.lastIndexOf("Success") > 0;         }     } 方法3 这两种方式没试过,他们利用的是系统的隐藏API,以后找个时间试下:     android静默安装apk     android应用后台安装,静默安装的代码实现方法     Android静默方式实现批量安装卸载应用程序 卸载APP 方法1 通过 Intent 机制,调出系统卸载应用。 Uri packageURI = Uri.parse("package:com.demo.test");   // 包名 Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);    startActivity(uninstallIntent); 方法2 直接调用卸载接口。 PackageInstallObserver observer = new PackageInstallObserver();   pm.installPackage( mPackageURI , observer, installFlags); 使用这种方法卸载应用需要权限: android.permission.DELETE_PACKAGES 打开APP private void openFile(File file) {                 // TODO Auto-generated method stub                 Log.e("OpenFile", file.getName());                 Intent intent = new Intent();                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                 intent.setAction(android.content.Intent.ACTION_VIEW);                 intent.setDataAndType(Uri.fromFile(file),                                 "application/vnd.android.package-archive");                 startActivity(intent);         } 下载APP protected File downLoadFile(String httpUrl) {                 // TODO Auto-generated method stub                 final String fileName = "updata.apk";                 File tmpFile = new File("/sdcard/update");                 if (!tmpFile.exists()) {                         tmpFile.mkdir();                 }                 final File file = new File("/sdcard/update/" + fileName);                   try {                         URL url = new URL(httpUrl);                         try {                                 HttpURLConnection conn = (HttpURLConnection) url                                                 .openConnection();                                 InputStream is = conn.getInputStream();                                 FileOutputStream fos = new FileOutputStream(file);                                 byte[] buf = new byte[256];                                 conn.connect();                                 double count = 0;                                 if (conn.getResponseCode() >= 400) {                                         Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)                                                         .show();                                 } else {                                         while (count <= 100) {                                                 if (is != null) {                                                         int numRead = is.read(buf);                                                         if (numRead <= 0) {                                                                 break;                                                         } else {                                                                 fos.write(buf, 0, numRead);                                                         }                                                 } else {                                                         break;                                                 }                                         }                                 }                                   conn.disconnect();                                 fos.close();                                 is.close();                         } catch (IOException e) {                                 // TODO Auto-generated catch block                                 e.printStackTrace();                         }                 } catch (MalformedURLException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                 }                   return file;         }

返回顶部
查看电脑版