AS:Android程序代码实现apk的安装和卸载(详细过程与代码)

源代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="卸载"
            android:onClick="uninstallClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="安装"
            android:onClick="installClick"/>
    </TableRow>
</TableLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  //    安装软件页面
    public void installClick(View v){
        //File file = new File(this.getFilesDir(), "mark.via.ui.browser.BrowserApp.apk");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //注意这里的app-release.apk为你要安装在模拟器上的apk名字
        String filePath="/sdcard/Download/app-release.apk";
        File file=new File(filePath);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //判读版本是否在7.0以上
        if (Build.VERSION.SDK_INT >= 24) {
            //provider authorities
            //com.example.intenttest为你的包名
            Uri apkUri = FileProvider.getUriForFile(this, "com.example.intenttest.fileprovider", file);
            //添加这句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
       startActivity(intent);
    }
    //    卸载软件页面
    public void uninstallClick(View v){
        Uri uri = Uri.fromParts("package", "com.example.intenttest", null);
        Intent it = new Intent(Intent.ACTION_DELETE, uri);
        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(it);
    }

}

AndriodManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intenttest">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.actions"
            android:resource="@xml/file_paths" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.intenttest.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
    <!--读写权限-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--安装卸载权限-->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

新建一个xml文件夹,在该文件夹下创建路径xml
在这里插入图片描述

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
        <external-path
            name="/download"
            path="Download"/>
</paths>

进行到这里可以运行一下项目,不报错即可
此时点击安装按钮会出现解析安装包错误

添加安装包到模拟器

首先运行你的项目(要运行起来),打开模拟器内部文件,方法如下:
在这里插入图片描述找到如下列表,将安装包放入此文件夹下
在这里插入图片描述导入:随便把其他项目已经打包好的apk导入进来就可以用。
在这里插入图片描述

打开模拟器相关权限

1.存储文件权限
2.安装未知应用权限
模拟器不同,界面略有区别
在这里插入图片描述
在这里插入图片描述

运行效果图

安装

在这里插入图片描述
在这里插入图片描述

卸载

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值