Andorid程序安装,替换及卸载操作
来源:互联网 发布:sra数据上传 编辑:程序博客网 时间:2024/06/14 23:54
/** * 安卓应用程序 APK安装,替换及卸载 * * @description: * @author ldm * @date 2016-4-27 下午4:53:01 */public class ApkOperatorActivity extends Activity { // 安装 private static final int INSTALL_REQUEST = 1; // 卸载 private static final int UNINSTALL_REQUEST = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.operator_apk_layout); initButtonsAndListeners(); } /** * 初始化Button及监听事件 * * @description: * @author ldm * @date 2016-4-27 下午4:29:12 */ private void initButtonsAndListeners() { Button button = (Button) findViewById(R.id.unknown_source); button.setOnClickListener(installApkListener); button = (Button) findViewById(R.id.my_source); button.setOnClickListener(installResultListener); button = (Button) findViewById(R.id.replace); button.setOnClickListener(replaceApkListener); button = (Button) findViewById(R.id.uninstall); button.setOnClickListener(uninstallApkListener); button = (Button) findViewById(R.id.uninstall_result); button.setOnClickListener(uninstallResultListener); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == INSTALL_REQUEST) { if (resultCode == Activity.RESULT_OK) { showToastMsg("安装成功!"); } else if (resultCode == Activity.RESULT_CANCELED) { showToastMsg("取消安装!"); } else { showToastMsg("安装失败!"); } } else if (requestCode == UNINSTALL_REQUEST) { if (resultCode == Activity.RESULT_OK) { showToastMsg("卸载成功!"); } else if (resultCode == Activity.RESULT_CANCELED) { showToastMsg("取消卸载!"); } else { showToastMsg("卸载失败!"); } } } private OnClickListener installApkListener = new OnClickListener() { public void onClick(View v) { // 安装APK Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(Uri.fromFile(checkApkFile("HelloActivity.apk"))); startActivity(intent); } }; private OnClickListener installResultListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(Uri.fromFile(checkApkFile("HelloActivity.apk"))); // 未知安装源 intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); // 返回结果 intent.putExtra(Intent.EXTRA_RETURN_RESULT, true); // 安装包的名称 intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, getApplicationInfo().packageName); startActivityForResult(intent, INSTALL_REQUEST); } }; private OnClickListener replaceApkListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(Uri.fromFile(checkApkFile("HelloActivity.apk"))); intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); intent.putExtra(Intent.EXTRA_RETURN_RESULT, true); // 替换安装包 intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true); intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, getApplicationInfo().packageName); startActivityForResult(intent, INSTALL_REQUEST); } }; private OnClickListener uninstallApkListener = new OnClickListener() { public void onClick(View v) { // 卸载 Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); intent.setData(Uri.parse("package:com.ldm.study.SmsActivity")); startActivity(intent); } }; private OnClickListener uninstallResultListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); intent.setData(Uri .parse("package:com.example.android.helloactivity")); // 设置卸载结果 intent.putExtra(Intent.EXTRA_RETURN_RESULT, true); startActivityForResult(intent, UNINSTALL_REQUEST); } }; private File checkApkFile(String assetName) { // 把测试的apk文件放到项目的Assets文件夹下面 byte[] buffer = new byte[8192]; InputStream is = null; FileOutputStream fos = null; try { is = getAssets().open(assetName); fos = openFileOutput("HelloActivity.apk", Context.MODE_WORLD_READABLE); int n; while ((n = is.read(buffer)) >= 0) { fos.write(buffer, 0, n); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { } try { if (fos != null) { fos.close(); } } catch (IOException e) { } } return getFileStreamPath("HelloActivity.apk"); } private void showToastMsg(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); }}
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10dip" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" android:text="演示如何使用APP安装包及意图Intent来安装一个应用程序。" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/unknown_source" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="安装APK" > </Button> <Button android:id="@+id/my_source" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="安装并提示结果" > </Button> <Button android:id="@+id/replace" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="替换APK" > </Button> <Button android:id="@+id/uninstall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="卸载APK" > </Button> <Button android:id="@+id/uninstall_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="卸载并提示结果" > </Button></LinearLayout>
**提示:要在项目的assets文件夹下放一个可安装的HelloActivity.apk文件,否则看不出效果。
开源代码:https://github.com/ldm520/ANDROID_API_DEMOS
0 0
- Andorid程序安装,替换及卸载操作
- Android 监听程序的安装、卸载、替换
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- 判断ARX程序是否加载及加载卸载操作
- 图文讲解VS2010程序打包操作 安装卸载
- Oracle操作及卸载
- 程序安装,升级及卸载核心部分(java编写)
- visual studio怎样打包制作安装及卸载程序
- Andorid 如何监听app是否安装或卸载
- Android 安装卸载程序
- 安装程序卸载
- 安装程序卸载
- Linux 安装、卸载程序
- Linux 安装、卸载程序
- Android应用启动界面分析(Starting Window)
- js中运算符的优先级
- android 控件 下拉刷新 FlyRefersh
- 写给曾经浮躁过和现在正浮躁的程序员
- 2011年山东ACM第二届省赛 Mathman Bank(模拟)
- Andorid程序安装,替换及卸载操作
- 彻底解决 Canvas 引起的 java.lang.unsupported operation exception, android.view.GLES20Canvas.clipPath(GLES20
- jQurey全选,反选,不全选功能实现
- SurfaceView介绍
- jquery autocomplete 插件的使用
- Android studio插件GsonFormat 的使用
- iOS学习笔记75-NSURLSession使用实战教程
- 【jQuery】 ajax 提交表单问题--双击会重复提交;success函数不执行
- 9.1 数据库事务概述