You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android桌面启动器实现卸载应用、应用信息按钮的方案咨询

功能实现方案

权限说明

两个功能都不需要申请任何特殊权限,也不需要调用PackageManager的额外方法,你只需构造对应系统Intent跳转即可,所有操作最终由用户在系统页面手动确认执行。

代码修改步骤

你在onCreateContextMenu中已经将当前列表项的位置存入了MenuItem的groupId参数,所以在选中事件中可以直接通过item.getGroupId()获取选中应用的下标,拿到对应的包名即可实现跳转。

修改后的onContextItemSelected代码

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
    // 获取选中的应用下标
    int selectedPos = item.getGroupId();
    String targetPackageName = appsList.get(selectedPos).getPackageName();
    // 若当前代码在Activity中可直接用this作为上下文
    Context context = itemView.getContext();

    switch (item.getItemId())
    {
        case 1: // 添加到桌面收藏
            displayMessage("Added to Favourites");
            return true;

        case 2: // 跳转应用信息系统页
            Intent infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            infoIntent.setData(Uri.parse("package:" + targetPackageName));
            context.startActivity(infoIntent);
            return true;

        case 3: // 跳转系统卸载页面
            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE);
            uninstallIntent.setData(Uri.parse("package:" + targetPackageName));
            context.startActivity(uninstallIntent);
            return true;

        default:
            displayMessage("You should not be seeing this message");
    }

    return super.onContextItemSelected(item);
}

注意事项

  • 别忘了导入android.provider.Settings和android.net.Uri两个系统类
  • 如果跳转的是系统预置应用,卸载页面会自动提示用户无法卸载,无需额外做兼容处理
  • Android 14及以上版本卸载功能也可以用Intent.ACTION_UNINSTALL_PACKAGE替换Intent.ACTION_DELETE,效果完全一致

内容的提问来源于stack exchange,提问作者Aksel Tahir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 00:24:03