Android程序使用代碼的安裝和卸載!!!

 

Android程序使用代碼的安裝和卸載!!! 

安裝:
 
  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);  
卸載:
  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程序代碼

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

 

 
//打開APK程序代碼

  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),"application/vnd.android.package-archive");  
  8.                 startActivity(intent);  
  9.         }  

出自:http://blog.csdn.net/shazhuzhux/article/details/6454729

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章