当前位置: 首页 > news >正文

关于 java+gradle的弹窗多选应用app

在这里插入图片描述在这里插入图片描述
已过滤系统应用

package com.example.skipAd;import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;import androidx.appcompat.app.AlertDialog;import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;/*** App 选择器工具类(支持多选 + 本地保存 + 回显)*/
public class AppChooserHelper {private final Context context;private final SharedPreferences prefs;private final String prefsKey;private List<String> appNames;private List<String> packageNames;public interface OnAppsSelectedListener {void onAppsSelected(Set<String> selectedApps);}/*** @param context   Activity 或 Application Context* @param prefsName SharedPreferences 名称* @param prefsKey  保存选择的 key*/public AppChooserHelper(Context context, String prefsName, String prefsKey) {this.context = context;this.prefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);this.prefsKey = prefsKey;}/*** 显示多选 App 弹窗*/public void showAppChooserDialog(final OnAppsSelectedListener listener) {final PackageManager pm = context.getPackageManager();List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);final Set<String> savedApps = prefs.getStringSet(prefsKey, new HashSet<>());final Set<String> selectedApps = new HashSet<>(savedApps);List<String> selectedAppNames = new ArrayList<>();List<String> otherAppNames = new ArrayList<>();List<String> selectedPackageNames = new ArrayList<>();List<String> otherPackageNames = new ArrayList<>();for (ApplicationInfo app : apps) {// 只显示可启动的第三方应用if (pm.getLaunchIntentForPackage(app.packageName) != null&& (app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {String name = app.loadLabel(pm).toString();String pkg = app.packageName;if (savedApps.contains(name)) {selectedAppNames.add(name);selectedPackageNames.add(pkg);} else {otherAppNames.add(name);otherPackageNames.add(pkg);}}}// 合并:已选择应用在前appNames = new ArrayList<>();appNames.addAll(selectedAppNames);appNames.addAll(otherAppNames);packageNames = new ArrayList<>();packageNames.addAll(selectedPackageNames);packageNames.addAll(otherPackageNames);// checkedItems 逻辑保持不变boolean[] checkedItems = new boolean[appNames.size()];for (int i = 0; i < appNames.size(); i++) {checkedItems[i] = savedApps.contains(appNames.get(i));}AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("选择应用").setMultiChoiceItems(appNames.toArray(new String[0]), checkedItems,(dialog, which, isChecked) -> {if (isChecked) {selectedApps.add(appNames.get(which));} else {selectedApps.remove(appNames.get(which));}}).setPositiveButton("确定", (dialog, which) -> {prefs.edit().putStringSet(prefsKey, selectedApps).apply();if (listener != null) {listener.onAppsSelected(selectedApps);}}).setNegativeButton("取消", null).show();}/*** 获取已选择的应用(回显用)*/public Set<String> getSelectedApps() {return prefs.getStringSet(prefsKey, new HashSet<>());}
}
http://www.dtcms.com/a/346344.html

相关文章:

  • 【GPT入门】第54课 量化位数与存储大小的影响
  • Java 面试题训练助手 Web 版本
  • 网络通信——UDP协议。
  • Kubernetes 1.28 集群部署指南(基于 Containerd 容器运行时)
  • 笔记:二叉树构建方法
  • 从“配置化思维”到“前端效率革命”:xiangjsoncraft 如何用 JSON 简化页面开发?
  • 【源码】MES系统:从下达计划、执行反馈、异常预警到过程控制的一整套执行中枢。
  • FastTracker:实时准确的视觉跟踪
  • 一键部署openGauss6.0.2轻量版单节点
  • DPY-3010: connections to this database server version are not supported by p
  • LoRA内幕机制解析(53)
  • Design Compiler:层次模型(Block Abstraction)的简介
  • 什么是神鸟云?
  • 亚马逊老品怎么再次爆发流量?
  • 软件测试要怎么自学?
  • CVPR 2025 | 哈工大港大DeCLIP:解耦CLIP注意力实现开放词汇感知!
  • RK3588随笔:MIPI协议——D-PHY 物理层的自定义和校验
  • codeforces round 1043(div3) 补题
  • Finite State Machine(FSM) for the Development Mode
  • NVM-Windows 命令大全
  • YOLO --- YOLOv5模型以及项目详解
  • Tiger任务管理系统-13
  • MiniOB环境部署开发(使用Docker)
  • FPC设计技巧
  • 解释实现哈希值作为唯一的ID以及后面的hexdigest是什么意思
  • 剑指数组相关
  • CSS自定义属性(CSS变量)
  • 全面解析 `strncasecmp` 字符串比较函数
  • ES6变量与解构:let、const与模板字符串全解析
  • 53 C++ 现代C++编程艺术2-枚举和枚举类