代码改变世界

Android 下载安装APK

2012-08-13 00:36  javaspring  阅读(362)  评论(0编辑  收藏  举报

代码安装apk

  1. Intent i = new Intent(Intent.ACTION_VIEW);
  2. String filePath = "/sdcard/XXX.apk";
  3. i.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");
  4. context.startActivity(i);
复制代码

程序卸载

  1. Intent intent = new Intent(Intent.ACTION_DELETE, uri);
  2. intent.startActivity();
复制代码

监听是否卸载成功

  1. DeleteReceiver mDeleteReceiver = new DeleteReceiver();//自定义的广播接收类,接收到结果后的操作
  2. IntentFilter filter = new IntentFilter(
  3. Intent.ACTION_PACKAGE_DATA_CLEARED);
  4. filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  5. filter.addDataScheme("package");
  6. registerReceiver(mDeleteReceiver, filter); //注册广播和过滤
  7. Intent undeleteIntent = new Intent(Intent.ACTION_DELETE, packageURI);
  8. startActivity(undeleteIntent);