Android程序使用代码的安装和卸载!!!
安装:
<wbr><p><span style="color:#ff6600"><strong>//下载apk程序代码<br></strong></span>protected File downLoadFile(String httpUrl) {<br> // TODO Auto-generated method stub<br> final String fileName = "updata.apk";<br> File tmpFile = new File("/sdcard/update");<br> if (!tmpFile.exists()) {<br> tmpFile.mkdir();<br> }<br> final File file = new File("/sdcard/update/" + fileName);<br><br> try {<br> URL url = new URL(httpUrl);<br> try {<br> HttpURLConnection conn = (HttpURLConnection) url<br> .openConnection();<br> InputStream is = conn.getInputStream();<br> FileOutputStream fos = new FileOutputStream(file);<br> byte[] buf = new byte[256];<br> conn.connect();<br> double count = 0;<br> if (conn.getResponseCode() >= 400) {<br> Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)<br> .show();<br> } else {<br> while (count <= 100) {<br> if (is != null) {<br> int numRead = is.read(buf);<br> if (numRead <= 0) {<br> break;<br> } else {<br> fos.write(buf, 0, numRead);<br> }<br><br> } else {<br> break;<br> }<br><br> }<br> }<br><br> conn.disconnect();<br> fos.close();<br> is.close();<br> } catch (IOException e) {<br> // TODO Auto-generated catch block<br><br> e.printStackTrace();<br> }<br> } catch (MalformedURLException e) {<br> // TODO Auto-generated catch block<br><br> e.printStackTrace();<br> }<br><br> return file;<br> }<br><strong><span style="color:#ff0000">//打开APK程序代码<br></span></strong><br> private void openFile(File file) {<br> // TODO Auto-generated method stub<br> Log.e("OpenFile", file.getName());<br> Intent intent = new Intent();<br> intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br> intent.setAction(android.content.Intent.ACTION_VIEW);<br> intent.setDataAndType(Uri.fromFile(file),<br> "application/vnd.android.package-archive");<br> startActivity(intent);<br> }</p> <p></p> <p></p> <p></p> <p></p> <p>转自:<a href="http://archive.cnblogs.com/a/1979268/">http://archive.cnblogs.com/a/1979268/</a>作者:<a href="http://www.cnblogs.com/Greenwood/" target="_blank">Greenwood</a></p> </wbr>
String str = "/CanavaCancel.apk";
String fileName = Environment.getExternalStorageDirectory() + str;
Intent intent = new Intent(Intent.ACTION_VIEW);
i
ntent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
卸载:
Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
Environment拥有一些可以获取环境变量的方法
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名).