自定义弹窗跳到app商店评价进行评分

本文介绍了一种在Android环境中获取已安装应用商店列表的方法,并实现了从应用内直接跳转到指定应用商店的详情页面的功能。通过过滤系统预装的应用商店,提供了更符合需求的应用商店列表。

//工具类

public class JumpEvaluateAppUtils {
    private List<AppInfo> appInfos;
    private Dialog signRuleDialog;
    public JumpEvaluateAppUtils(Context context) {
        ArrayList<String> list= getFilterInstallMarkets(context,InstalledAPPs(context));
        for (int i = 0; i < list.size(); i++) {
            Log.e("JumpEvaluateAppUtils",list.get(i));
        }
        signRule(context);
        signRuleDialog.show();
    }
    /**
     * 获取已安装应用商店的包名列表
     *
     * @param context
     * @return
     */
    public static ArrayList<String> InstalledAPPs(Context context) {
        ArrayList<String> pkgs = new ArrayList<String>();
        if (context == null)
            return pkgs;
        Intent intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.APP_MARKET");
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        if (infos == null || infos.size() == 0)
            return pkgs;
        int size = infos.size();
        for (int i = 0; i < size; i++) {
            String pkgName = "";
            try {
                ActivityInfo activityInfo = infos.get(i).activityInfo;
                pkgName = activityInfo.packageName;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!TextUtils.isEmpty(pkgName))
                pkgs.add(pkgName);

        }
        return pkgs;
    }

    public ArrayList<String> getFilterInstallMarkets(Context context, ArrayList<String> pkgs) {
        appInfos = new ArrayList<>();
        appInfos.clear();
        ArrayList<String> appList = new ArrayList<String>();
        if (context == null || pkgs == null || pkgs.size() == 0)
            return appList;
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> installedPkgs = pm.getInstalledPackages(0);
        int li = installedPkgs.size();
        int lj = pkgs.size();
        for (int j = 0; j < lj; j++) {
            for (int i = 0; i < li; i++) {
                String installPkg = "";
                String checkPkg = pkgs.get(j);
                PackageInfo packageInfo = installedPkgs.get(i);
                try {
                    installPkg = packageInfo.packageName;

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TextUtils.isEmpty(installPkg))
                    continue;
                if (installPkg.equals(checkPkg)) {
                    // 如果非系统应用,则添加至appList,这个会过滤掉系统的应用商店,如果不需要过滤就不用这个判断
//                    if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                        //将应用相关信息缓存起来,用于自定义弹出应用列表信息相关用
                        AppInfo appInfo = new AppInfo();
                        appInfo.setAppName(packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
                        appInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(context.getPackageManager()));
                        appInfo.setPackageName(packageInfo.packageName);
                        appInfo.setVersionCode(packageInfo.versionCode);
                        appInfo.setVersionName(packageInfo.versionName);
                        Log.e("JumpEvaluateAppUtils",appInfo.toString());
                        appInfos.add(appInfo);
                        appList.add(installPkg);
//                    }
                    break;
                }

            }
        }
        return appList;
    }

    /**
     * 跳转到app详情界面
     * @param appPkg App的包名
     * @param marketPkg
     *  应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面,某些应用商店可能会失败
     */
    public static void launchAppDetail(Context context, String appPkg, String marketPkg) {
        try {
            if (TextUtils.isEmpty(appPkg))
                return;
            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(marketPkg))
                intent.setPackage(marketPkg);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void signRule(final Context context) {
        signRuleDialog = new Dialog(context, R.style.MyDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.appinfo_dialog, null);
        GridView gv = (GridView) view.findViewById(R.id.jump_gv);
        gv.setAdapter(new JumpAdapter(context,appInfos));
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                launchAppDetail(context,context.getPackageName(),appInfos.get(position).getPackageName());
                signRuleDialog.dismiss();
            }
        });
        Window window = signRuleDialog.getWindow();
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
        signRuleDialog.setContentView(view, layoutParams);
    }
}

//bean类

public class AppInfo {
    private String AppName;
    private Drawable AppIcon;
    private String PackageName;
    private int VersionCode;
    private String VersionName;

    public String getAppName() {
        return AppName;
    }

    public void setAppName(String appName) {
        AppName = appName;
    }

    public Drawable getAppIcon() {
        return AppIcon;
    }

    public void setAppIcon(Drawable appIcon) {
        AppIcon = appIcon;
    }

    public String getPackageName() {
        return PackageName;
    }

    public void setPackageName(String packageName) {
        PackageName = packageName;
    }

    public int getVersionCode() {
        return VersionCode;
    }

    public void setVersionCode(int versionCode) {
        VersionCode = versionCode;
    }

    public String getVersionName() {
        return VersionName;
    }

    public void setVersionName(String versionName) {
        VersionName = versionName;
    }

    @Override
    public String toString() {
        return "AppInfo{" +
                "AppName='" + AppName + '\'' +
                ", AppIcon=" + AppIcon +
                ", PackageName='" + PackageName + '\'' +
                ", VersionCode=" + VersionCode +
                ", VersionName='" + VersionName + '\'' +
                '}';
    }
}
//adapter

public class JumpAdapter extends BaseAdapter {

    Context mContext;
    List<AppInfo> mData;

    public JumpAdapter(Context context, List<AppInfo> list) {

        this.mContext = context;
        this.mData = list;

    }

    @Override
    public int getCount() {

        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {

        ViewHolder1 viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder1();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.jump_item, null);
            viewHolder.jump_iv = (ImageView) convertView
                    .findViewById(R.id.jump_iv);
            viewHolder.jump_name = (TextView) convertView
                    .findViewById(R.id.jump_name);
            viewHolder.jump_iv.setImageDrawable(mData.get(position).getAppIcon());
            viewHolder.jump_name.setText(mData.get(position).getAppName());
        } else {
            viewHolder = (ViewHolder1) convertView.getTag();
        }

        return convertView;
    }
}

class ViewHolder1 {
    ImageView jump_iv;
    TextView jump_name;
}
//style

<style name="MyDialog" parent="android:Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
</style>

//appinfo_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
     <LinearLayout
         android:background="@color/colorAccent"
         android:layout_centerInParent="true"
         android:layout_width="250dp"
         android:layout_height="200dp">
         <GridView
             android:id="@+id/jump_gv"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:numColumns="3"></GridView>
     </LinearLayout>
</RelativeLayout>
//jump_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/jump_ll"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     <ImageView
         android:id="@+id/jump_iv"
         android:layout_gravity="center_horizontal"
         android:src="@mipmap/ic_launcher"
         android:layout_width="50dp"
         android:layout_height="50dp" />
    <TextView
        android:id="@+id/jump_name"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"/>
</LinearLayout>
//调用

new JumpEvaluateAppUtils(MainActivity.this);

from appium import webdriver from appium.options.android import UiAutomator2Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException, NoSuchElementException import time import os # ==================== 配置参数 ==================== APK_PATH = r"C:\own\download\iPL5.3.10_signed.apk" APP_PACKAGE = "com.brother.ptouch.iprintandlabel" APP_ACTIVITY = ".module.home.home.HomeActivity" MAX_BACK_OFFICE_COUNT = 3 MAX_RUNTIME = 600 # 最大运行时间:10分钟 # ==================== 创建 WebDriver ==================== def create_driver(): options = UiAutomator2Options() options.set_capability("platformName", "Android") options.set_capability("deviceName", "emulator-5554") # 改成你的设备名(adb devices 查看) options.set_capability("automationName", "UiAutomator2") # ✅ 关键修复:指定 APK 文件路径,用于 noReset=False 时自动重装 options.set_capability("app", APK_PATH) # 启动配置 options.set_capability("appPackage", APP_PACKAGE) options.set_capability("appActivity", APP_ACTIVITY) options.set_capability("noReset", False) # 每次清空状态(需配合 app 使用) options.set_capability("fullReset", False) # 不完全重置(不清除缓存) options.set_capability("autoGrantPermissions", True) options.set_capability("autoAcceptAlerts", True) # 自动接受权限弹窗 options.set_capability("newCommandTimeout", 600) return webdriver.Remote('http://localhost:4723', options=options) # ==================== 卸载应用(用于重置状态)==================== def reset_app_state(driver): print("🧹 正在重置应用状态...") if driver.is_app_installed(APP_PACKAGE): try: driver.remove_app(APP_PACKAGE) print("✅ 应用已卸载,状态重置完成") except Exception as e: print(f"⚠️ 卸载失败: {e}") else: print("ℹ️ 应用未安装,无需卸载") # ==================== 主探索类 ==================== class AndroidAppExplorer: def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(driver, 10) self.clicked_identifiers = set() self.recovery_history = [] self.start_time = time.time() def wait_for_page_load(self, timeout=10): """等待页面基本结构加载""" try: WebDriverWait(self.driver, timeout).until( EC.presence_of_element_located(( 'xpath', '//*[contains(@resource-id, "layout") or @class="android.widget.LinearLayout"]' )) ) time.sleep(1) except: pass def get_element_identifier(self, element): """生成唯一可读标识符""" parts = [] res_id = element.get_attribute("resourceId") text = element.get_attribute("text") content_desc = element.get_attribute("contentDescription") clazz = element.get_attribute("className") if res_id: parts.append(f"resId={res_id.split('/')[-1]}") if text and len(text) < 20: parts.append(f"text='{text}'") if content_desc: parts.append(f"desc='{content_desc}'") if clazz: simple_class = clazz.split('.')[-1] if simple_class not in ['View', 'ViewGroup']: parts.append(f"class={simple_class}") return " | ".join(parts) if parts else "unknown" def find_clickable_elements(self): """查找所有可点击且可见的元素""" xpath = '//*[(@clickable="true" or @focusable="true") and @displayed="true" and @enabled="true"]' try: elements = self.driver.find_elements('xpath', xpath) return [e for e in elements if e.is_displayed()] except: return [] def ensure_element_visible(self, element): """通过滑动确保元素可见""" try: rect = element.rect win_size = self.driver.get_window_size() y_center = rect['y'] + rect['height'] / 2 height = win_size['height'] if y_center < 0.1 * height or y_center > 0.9 * height: self.driver.swipe(win_size['width'] // 2, height * 0.7, win_size['width'] // 2, height * 0.3, 600) time.sleep(0.5) except: pass def click_element_safely(self, element): """安全点击元素,防止重复点击""" identifier = self.get_element_identifier(element) if identifier in self.clicked_identifiers: return False try: self.ensure_element_visible(element) element.click() print(f"🟢 点击成功: {identifier}") self.clicked_identifiers.add(identifier) time.sleep(1.5) return True except Exception as e: print(f"🔴 点击失败: {identifier}, 错误: {str(e)}") return False def is_popup_visible(self): """判断是否出浮层菜单(如 Spinner 下拉)""" try: popup_classes = [ 'android.widget.PopupWindow', 'android.widget.ListPopupWindow' ] for cls in popup_classes: if self.driver.find_elements('class name', cls): return True items = self.driver.find_elements('class name', 'android.widget.CheckedTextView') for item in items: if item.is_displayed(): bounds = item.get_attribute("bounds") try: y1 = int(bounds.split(',')[1].split(']')[0]) if 200 < y1 < 1000: return True except: pass return False except: return False def handle_floating_popup(self): """处理浮层选择项(如 High / Low)""" if not self.is_popup_visible(): return False print("🎈 检测到浮层,尝试选择...") common_options = ["High", "Normal", "Low", "On", "Off", "Auto", "Cut Every Label"] for opt in common_options: try: elem = self.driver.find_element(By.XPATH, f'//android.widget.CheckedTextView[@text="{opt}"]') if elem.is_displayed(): elem.click() print(f"🪄 选择了浮层选项: {opt}") time.sleep(1.5) return True except: continue try: first_item = self.driver.find_element( By.XPATH, '//android.widget.CheckedTextView[@displayed="true"][1]' ) first_item.click() print("🪄 选择了默认浮层项") time.sleep(1.5) return True except: print("❌ 无法选择浮层项,使用 back 关闭") try: self.driver.back() time.sleep(1) except: pass return False def handle_alert_popup(self): """处理系统或应用弹窗(如权限请求)""" alert_xpaths = [ '//android.widget.Button[contains(@text, "OK")]', '//android.widget.Button[contains(@text, "允许")]', '//android.widget.Button[contains(@text, "Allow")]', '//android.widget.Button[@text="YES"]' ] for xpath in alert_xpaths: try: btn = self.driver.find_element('xpath', xpath) if btn.is_displayed(): btn.click() print("✅ 自动处理了弹窗") time.sleep(1) return True except: continue return False def ensure_in_target_app(self): """确保当前在目标 App,支持短暂跳转(如浏览器)""" current_pkg = self.driver.current_package current_act = self.driver.current_activity if current_pkg == APP_PACKAGE: self.wait_for_page_load(1) return True allowed_packages = ["nexuslauncher", "chrome", "android", "packageinstaller"] if any(pkg in current_pkg.lower() for pkg in allowed_packages): print(f"⏸️ 暂时处于外部页面: {current_pkg}/{current_act}") key = (current_pkg, current_act) if self.recovery_history[-10:].count(key) >= 3: print("🛑 恢复循环检测,终止") return False self.recovery_history.append(key) try: self.driver.back() time.sleep(2) if self.driver.current_package == APP_PACKAGE: print("✅ 通过 back 返回目标 App") return True except: pass try: self.driver.activate_app(APP_PACKAGE) time.sleep(3) return self.driver.current_package == APP_PACKAGE except: return False return False def accept_user_agreement(self): """处理首次启动的用户协议(自动滚动 + 点击 I agree)""" print("🔍 检测用户协议...") try: WebDriverWait(self.driver, 15).until( EC.presence_of_element_located(( 'xpath', '//*[contains(@text, "License Agreement") or contains(@text, "User")]' )) ) print("📄 用户协议页面已加载") def find_agree_button(): candidates = [ '//android.widget.Button[@text="Agree"]', '//android.widget.Button[@text="I agree"]', '//android.widget.Button[contains(@resource-id, "yes")]', ] for xpath in candidates: try: btn = self.driver.find_element(By.XPATH, xpath) if btn.is_displayed() and btn.is_enabled(): return btn except: continue return None agree_btn = find_agree_button() if agree_btn: print("✅ 发现【I agree】按钮,直接点击") agree_btn.click() time.sleep(3) return True print("👇 正在滚动协议内容...") for i in range(10): size = self.driver.get_window_size() x = size['width'] // 2 y_start = int(size['height'] * 0.8) y_end = int(size['height'] * 0.3) self.driver.swipe(x, y_start, x, y_end, 600) time.sleep(0.8) agree_btn = find_agree_button() if agree_btn: print(f"✅ 第 {i + 1} 次滑动后发现可点击的【I agree】按钮") agree_btn.click() print("✅ 已点击【I agree】") time.sleep(3) return True try: self.driver.find_element( 'android uiautomator', 'new UiSelector().textContains("agree").clickable(true)' ).click() print("🪄 使用 uiautomator 强制点击【I agree】") time.sleep(3) return True except: print("❌ 无法找到或点击【I agree】按钮") return False except TimeoutException: print("🟢 未检测到用户协议,跳过") return True except Exception as e: print(f"⚠️ 协议处理异常: {type(e).__name__}: {e}") return False def handle_printer_selection(self): """处理 Brother 打印机选择页面(兼容无标题情况)""" print("🖨️ 检测打印机选择页面(基于设备型号+Done按钮)...") # 设备型号关键字 device_keywords = ["PT-", "TD-", "QL-", "HL-", "MFC-"] try: # 强制等待至少一个设备出现(最多 10 秒) WebDriverWait(self.driver, 10).until( lambda d: any( any(kw in (elem.get_attribute("text") or "") for kw in device_keywords) for elem in d.find_elements( By.XPATH, '//android.widget.TextView[@text and string-length(@text) > 5]' ) ) ) print("✅ 进入打印机选择页面") # 找第一个符合设备命名规则的 TextView 并点击 candidates = self.driver.find_elements( By.XPATH, '//android.widget.TextView[@text and string-length(@text) > 5]' ) selected = False for elem in candidates: text = elem.get_attribute("text") or "" if any(kw in text for kw in device_keywords): try: elem.click() print(f"✅ 选择了打印机: {text}") time.sleep(1.5) selected = True break except: print(f"🟡 无法点击 {text}(可能已选中)") selected = True break if not selected: print("ℹ️ 未点击新设备(可能已有默认选中)") # 点击 Done done_btn = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(( By.XPATH, '//android.widget.Button[@text="Done" or @resource-id="com.brother.ptouch.iprintandlabel:id/wel_download_templates"]' )) ) done_btn.click() print("✅ 成功点击【Done】进入主界面") time.sleep(3) return True except TimeoutException: print("🟢 未检测到打印机选择页面,跳过") return True except Exception as e: print(f"⚠️ 打印机选择异常: {e}") return True def explore_page(self): """主探索逻辑""" print("\n🔄 开始探索当前页面...") last_handled_popup = False explored_states = set() consecutive_back_count = 0 while True: if time.time() - self.start_time > MAX_RUNTIME: print("⏰ 探索超时,自动结束。") break if not self.ensure_in_target_app(): print("❌ 无法恢复至目标应用") break self.handle_alert_popup() if self.is_popup_visible() and not last_handled_popup: self.handle_floating_popup() last_handled_popup = True time.sleep(1) continue else: last_handled_popup = False elements = self.find_clickable_elements() if not elements: print("📭 当前页面无可点击元素") try: self.driver.back() print("🔙 返回上一页") time.sleep(2) consecutive_back_count += 1 if consecutive_back_count > MAX_BACK_OFFICE_COUNT: print("🏁 所有路径已探索完毕") break continue except: break current_act = self.driver.current_activity element_fingerprint = "|".join(sorted([self.get_element_identifier(e) for e in elements[:5]])) page_key = f"{current_act}|{element_fingerprint[:60]}" if page_key in explored_states: print(f"🟡 已探索过此页面: {current_act}") try: self.driver.back() time.sleep(2) consecutive_back_count += 1 if consecutive_back_count > MAX_BACK_OFFICE_COUNT: print("🔚 探索完成") break continue except: break else: explored_states.add(page_key) consecutive_back_count = 0 clicked = False for elem in elements: if self.click_element_safely(elem): clicked = True break if not clicked: print("✅ 当前页无新元素可操作") try: self.driver.back() time.sleep(2) except: break # ==================== 主程序入口 ==================== if __name__ == "__main__": driver = None try: print("🚀 启动 Android 自动化测试架...") # Step 1: 创建驱动(会自动安装 APK) driver = create_driver() # Step 2: 启动应用(create_driver 已启动) print("📱 应用正在启动...") time.sleep(5) # Step 3: 创建探索器并处理初始化流程 explorer = AndroidAppExplorer(driver) # 处理用户协议 if not explorer.accept_user_agreement(): print("❌ 用户协议处理失败,终止运行") else: # 处理打印机选择 explorer.handle_printer_selection() # 开始探索 explorer.explore_page() except KeyboardInterrupt: print("\n👋 用户中断执行") except Exception as e: print(f"💥 运行异常: {type(e).__name__}: {e}") finally: # 清理:卸载应用 + 关闭会话 if driver: reset_app_state(driver) driver.quit() print("🔚 自动化流程全部完成") 我的代码是怎么实现的桌面进入app,用户协议、打印机选择,并在结束后重置APP?
最新发布
10-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值