Andorid程序安装,替换及卸载操作

/**
 * 安卓应用程序 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值