Android 程序使用代码的安装和卸载

安装:

[java]  view plain  copy
  1. String str = "/CanavaCancel.apk";  
  2. String fileName = Environment.getExternalStorageDirectory() + str;  
  3. Intent intent = new Intent(Intent.ACTION_VIEW);  
  4. intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");  
  5. startActivity(intent);  
卸载:

[java]  view plain  copy
  1. Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");     
  2. Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);     
  3. startActivity(uninstallIntent);  
Environment拥有一些可以获取环境变量的方法 
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名).

//下载apk程序代码

[java]  view plain  copy
  1. protected File downLoadFile(String httpUrl) {  
  2.     // TODO Auto-generated method stub  
  3.     final String fileName = "updata.apk";  
  4.     File tmpFile = new File("/sdcard/update");  
  5.     if (!tmpFile.exists()) {  
  6.             tmpFile.mkdir();  
  7.     }  
  8.     final File file = new File("/sdcard/update/" + fileName);  
  9.   
  10.     try {  
  11.             URL url = new URL(httpUrl);  
  12.             try {  
  13.                     HttpURLConnection conn = (HttpURLConnection) url  
  14.                                     .openConnection();  
  15.                     InputStream is = conn.getInputStream();  
  16.                     FileOutputStream fos = new FileOutputStream(file);  
  17.                     byte[] buf = new byte[256];  
  18.                     conn.connect();  
  19.                     double count = 0;  
  20.                     if (conn.getResponseCode() >= 400) {  
  21.                             Toast.makeText(Main.this"连接超时", Toast.LENGTH_SHORT)  
  22.                                             .show();  
  23.                     } else {  
  24.                             while (count <= 100) {  
  25.                                     if (is != ) {  
  26.                                             int numRead = is.read(buf);  
  27.                                             if (numRead <= 0) {  
  28.                                                     break;  
  29.                                             } else {  
  30.                                                     fos.write(buf, 0, numRead);  
  31.                                             }  
  32.   
  33.                                     } else {  
  34.                                             break;  
  35.                                     }  
  36.   
  37.                             }  
  38.                     }  
  39.   
  40.                     conn.disconnect();  
  41.                     fos.close();  
  42.                     is.close();  
  43.             } catch (IOException e) {  
  44.                     // TODO Auto-generated catch block  
  45.   
  46.                     e.printStackTrace();  
  47.             }  
  48.     } catch (MalformedURLException e) {  
  49.             // TODO Auto-generated catch block  
  50.   
  51.             e.printStackTrace();  
  52.     }  
  53.   
  54.     return file;  
  55. }  
//打开APK程序代码

[java]  view plain  copy
  1. private void openFile(File file) {  
  2.         // TODO Auto-generated method stub  
  3.         Log.e("OpenFile", file.getName());  
  4.         Intent intent = new Intent();  
  5.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  6.         intent.setAction(android.content.Intent.ACTION_VIEW);  
  7.         intent.setDataAndType(Uri.fromFile(file),  
  8.                         "application/vnd.android.package-archive");  
  9.         startActivity(intent);  
  10. }  

猜你喜欢

转载自blog.csdn.net/try_zp_catch/article/details/80424438