用代码安装apk,用代码卸载一个应用

在系统安装的的底层常规应用中,是通过PackageInstaller来识别程序安装apk:


/**
  * 安装apk
  * @param file
  */
 private void install(File file){
  //激活系统的安装组件
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_VIEW);
  //设置数据和数据类型 
  intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  finish();
  startActivity(intent);
 }

 

PackageInstaller服务将会在内部匹配MIME类型为application/vnd.android.package-archive的intent,要说明的是setDataAndType的参数第一个为:URI,其实URI内部的String对应为file://加上常规路径,比如:file:///sdcard/test.apk,当然其他的Uri构造方法还可以Uri.fromFile(new File(mFileFullPath)).

底层PackageInstallerActivity在配置文件中是这样配置的:

  <activity android:name=".PackageInstallerActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="content" />
                <data android:scheme="file" />
                <data android:mimeType="application/vnd.android.package-archive" />
            </intent-filter>
        </activity>

有篇文章讲的和很深入http://www.2cto.com/kf/201403/285212.html

 

 

通过代码卸载一个apk:

Uri packageURI = Uri.parse("package:cn.itcast.mobilesafe");   
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   
startActivity(uninstallIntent);


 <activity android:name=".UninstallerActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.DELETE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="package" />
            </intent-filter>
        </activity>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 代码中使用 pm 命令安装 APK 的方法如下: 1. 在 Android 设备上启用开发者选项。您可以在设备设置中找到此选项,并启用它。 2. 在 Android 代码中,使用以下命令执行 pm 命令: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm install /path/to/apk/file.apk\n"); os.flush(); ``` 其中,`/path/to/apk/file.apk` 是您要安装APK 文件的路径。请注意,您需要将此路径替换为实际的 APK 文件路径。 如果您不知道 APK 文件的路径,可以使用以下命令列出设备上所有已安装应用程序的包名: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm list packages\n"); os.flush(); ``` 这将返回一个包含所有已安装应用程序的包名列表。您可以在列表中找到您要安装应用程序的包名,并使用以下命令它: ```java Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes("pm uninstall package_name\n"); os.flush(); ``` 其中,`package_name` 是应用程序的包名,可以在 AndroidManifest.xml 文件中找到。 请注意,在使用 pm 命令安装应用程序之前,请确保您已经连接了 Android 设备并启用了 USB 调试选项。您可以在设备设置中找到此选项,并启用它。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值