安装:
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" ); |
卸载:
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程序代码
查看源码 打印 ?
01 | protected File downLoadFile(String httpUrl) { |
03 |
final String fileName = "updata.apk" ; |
04 |
File tmpFile = new File( "/sdcard/update" ); |
05 |
if (!tmpFile.exists()) { |
08 |
final File file = new File( "/sdcard/update/" + fileName); |
11 |
URL url = new URL(httpUrl); |
13 |
HttpURLConnection conn = (HttpURLConnection) url |
15 |
InputStream is = conn.getInputStream(); |
16 |
FileOutputStream fos = new FileOutputStream(file); |
17 |
byte [] buf = new byte [ 256 ]; |
20 |
if (conn.getResponseCode() >= 400 ) { |
21 |
Toast.makeText(Main. this , "连接超时" , Toast.LENGTH_SHORT) |
26 |
int numRead = is.read(buf); |
30 |
fos.write(buf, 0 , numRead); |
43 |
} catch (IOException e) { |
48 |
} catch (MalformedURLException e) { |
58 | private void openFile(File file) { |
60 |
Log.e( "OpenFile" , file.getName()); |
61 |
Intent intent = new Intent(); |
62 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
63 |
intent.setAction(android.content.Intent.ACTION_VIEW); |
64 |
intent.setDataAndType(Uri.fromFile(file), |
65 |
"application/vnd.android.package-archive" ); |
66 |
startActivity(intent); |
- Android(安卓)Http请求的代码
- 更新Anadroid SDK Tooks之后,Eclipse提示No update were found
- 更新Anadroid SDK Tooks之后,Eclipse提示No update were found
- Android:安装APK包以后系统文件目录的变化
- Android(安卓)SDK: sdkmanager 命令行工具的使用(查看、安装、更
- Android设备管理器漏洞
- 基于TCP/IP协议的Java服务端与Android客户端的Socket通信及数据
- android 中http请求的容错管理
- Android(安卓)Accessibility(辅助功能) --实现Android应用自动安
随机推荐
-
Swoole在PHP-fpm/apache中使用task功能
-
php-fpm的reload过程
-
关于PHP安全编程的一些建议
-
php实现特殊字符的替换操作
-
关于PHP中Exception、Error Handler的细
-
PHP实现动态规划之背包问题
-
使用PHP来获取客户端和服务端IP
-
PHP 是怎么接收到请求的?
-
关于mysql操作的详细总结以及实例(一)
-
PHP之十六个魔术方法详解(总结)