Android代码实现APK文件的安装与卸载

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

安装:
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:程序完整的路径 (包名+程序名).
<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() &gt;= 400) {<br> Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)<br> .show();<br> } else {<br> while (count &lt;= 100) {<br> if (is != null) {<br> int numRead = is.read(buf);<br> if (numRead &lt;= 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值