安装:

Java代码
  1. Stringstr="/CanavaCancel.apk";
  2. StringfileName=Environment.getExternalStorageDirectory()+str;
  3. Intentintent=newIntent(Intent.ACTION_VIEW);
  4. intent.setDataAndType(Uri.fromFile(newFile(fileName)),"application/vnd.android.package-archive");
  5. startActivity(intent);

卸载:

Java代码
  1. UripackageURI=Uri.parse("package:com.demo.CanavaCancel");
  2. IntentuninstallIntent=newIntent(Intent.ACTION_DELETE,packageURI);
  3. startActivity(uninstallIntent);
Environment拥有一些可以获取环境变量的方法

package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名). 下载apk程序代码: Java代码
  1. protectedFiledownLoadFile(StringhttpUrl){
  2. //TODOAuto-generatedmethodstub
  3. finalStringfileName="updata.apk";
  4. FiletmpFile=newFile("/sdcard/update");
  5. if(!tmpFile.exists()){
  6. tmpFile.mkdir();
  7. }
  8. finalFilefile=newFile("/sdcard/update/"+fileName);
  9. try{
  10. URLurl=newURL(httpUrl);
  11. try{
  12. HttpURLConnectionconn=(HttpURLConnection)url
  13. .openConnection();
  14. InputStreamis=conn.getInputStream();
  15. FileOutputStreamfos=newFileOutputStream(file);
  16. byte[]buf=newbyte[256];
  17. conn.connect();
  18. doublecount=0;
  19. if(conn.getResponseCode()>=400){
  20. Toast.makeText(Main.this,"连接超时",Toast.LENGTH_SHORT)
  21. .show();
  22. }else{
  23. while(count<=100){
  24. if(is!=null){
  25. intnumRead=is.read(buf);
  26. if(numRead<=0){
  27. break;
  28. }else{
  29. fos.write(buf,0,numRead);
  30. }
  31. }else{
  32. break;
  33. }
  34. }
  35. }
  36. conn.disconnect();
  37. fos.close();
  38. is.close();
  39. }catch(IOExceptione){
  40. //TODOAuto-generatedcatchblock
  41. e.printStackTrace();
  42. }
  43. }catch(MalformedURLExceptione){
  44. //TODOAuto-generatedcatchblock
  45. e.printStackTrace();
  46. }
  47. returnfile;
  48. }
打开APK程序代码: Java代码
  1. privatevoidopenFile(Filefile){
  2. //TODOAuto-generatedmethodstub
  3. Log.e("OpenFile",file.getName());
  4. Intentintent=newIntent();
  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. }

更多相关文章

  1. android EditText设置不可写
  2. android 使用html5作布局文件: webview跟javascript交互
  3. android studio调试c/c++代码
  4. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
  5. 锁屏界面
  6. android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
  7. Android(安卓)version and Linux Kernel version
  8. Android(安卓)闹钟管理类的使用
  9. Android学习篇之Menu的使用

随机推荐

  1. Asp.Net MVC实现分页、检索、排序的代码
  2. asp如何解析json字符串并转化为asp对象
  3. asp读取excel表格第x行第y列值
  4. ASP.NET中的Web.config配置文件介绍
  5. 使用asp.net mvc使用JsonResult返回Json
  6. 解析ADO.NET对SQL Server数据库执行增删
  7. 验证码类在asp.net mvc中的使用
  8. 在asp.net中使用JQuery Ajax相关用法总结
  9. 分析.NET的异常处理
  10. 介绍ASP.NET中的MVC如何从控制器传递数据