用自己的APP打开微信和支付宝付款码和扫一扫界面

1.调用支付宝扫一扫界面:

Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

2.调用支付宝收款码界面:

Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=20000056");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

3.调用微信扫一扫界面:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
intent.setFlags(335544320);
intent.setAction("android.intent.action.VIEW");
startActivity(intent);

4.调用微信收款码,这里需要使用辅助功能实现

1.在AndroidMainfest.xml中注册辅助功能

<service
            android:name=".PayAccessibility"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="location"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/pay_accessibility_config" />
</service>

2.在res下的xml配置服务config, 即pay_accessibility_config

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowStateChanged|typeViewFocused|typeViewClicked"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:canRequestFilterKeyEvents ="true"
    android:notificationTimeout="100"
    android:packageNames="@null"
    android:description="@string/accessibility_description"/>

3.辅助功能具体实现

package com.sanyesan.paymodel;

import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

/**
 * author         :MrXie
 * date           :2021/9/12 18:56
 * description    :
 */
public class PayAccessibility extends AccessibilityService {

    private static final String TAG = PayAccessibility.class.getSimpleName();
    private Intent intent = null;
    private boolean isPaymentReceivedClick = false;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        Log.d(TAG, "onAccessibilityEvent: 活动事件"+event.getEventType());
        switch (eventType){
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                break;
            case AccessibilityEvent.TYPE_VIEW_FOCUSED:
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                Log.d(TAG, "onAccessibilityEvent: 包名:"+event.getPackageName());
                ComponentName componentName = new ComponentName(event.getPackageName() == null ? "" : event.getPackageName().toString(), event.getClassName() == null ? "" : event.getClassName().toString());
                ActivityInfo activityInfo = tryGetActivity(componentName);
                if (event.getPackageName() != null && event.getPackageName().equals("com.tencent.mm")){
                    Log.d(TAG, "onAccessibilityEvent: 活动名:"+activityInfo);
                    boolean isActivity = activityInfo != null;
                    if(isActivity){
                        Log.d(TAG, "onAccessibilityEvent: 界面不为空");
                        if(TextUtils.equals("com.tencent.mm.ui.LauncherUI", activityInfo.name) && !isPaymentReceivedClick){
                            AccessibilityNodeInfo rootNode = getRootInActiveWindow();
                            if (rootNode != null) {
                                pay:for (AccessibilityNodeInfo node : rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/fcu")) {
                                    Log.d(TAG, "onAccessibilityEvent: 下拉列表");
                                    if (node.isEnabled() && TextUtils.equals(node.getClassName(), "android.widget.RelativeLayout")) {
                                        Log.d(TAG, "autoScreen: 自动点击");
                                        node.performAction(AccessibilityNodeInfo.ACTION_CLICK);

                                        //睡眠一会,等待视图刷新
                                        try {
                                            Thread.sleep(200);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        AccessibilityNodeInfo rootNodePayActivity = getRootInActiveWindow();
                                        Log.d(TAG, "onAccessibilityEvent: rootNodePayActivity.size = "+rootNodePayActivity.getChildCount());
                                        for (AccessibilityNodeInfo payNode : rootNodePayActivity.findAccessibilityNodeInfosByText("收付款")){//收付款按钮不可点击
                                            Log.d(TAG, "onAccessibilityEvent: 进入首付款界面:"+payNode.getClassName());
                                            if(payNode.isEnabled() &&  TextUtils.equals(payNode.getClassName(), "android.widget.TextView")){
                                                Log.d(TAG, "onAccessibilityEvent: 点击收付款按钮");
                                                payNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);//获取父级的控件
//                                                    stopSelf();
                                                isPaymentReceivedClick = true;
                                                Log.d(TAG, "onAccessibilityEvent: 停止服务");
                                            }
                                        }
                                        Log.d(TAG, "onAccessibilityEvent: 结束列表框遍历");
                                    }
                                }
                            }else {
                                Log.d(TAG, "autoScreen: 节点为空");
                            }
                        }
                    }else {
                        Log.d(TAG, "onAccessibilityEvent: 界面为空");
                    }
                }
                break;
            case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
                Log.d("EEE", "TYPE_WINDOWS_CHANGED: " + event.getPackageName() + " " + event.getClassName() + " " + event);
                break;
        }
    }

    /**
     * 获取Activity界面信息
     * @param componentName
     * @return
     */
    private ActivityInfo tryGetActivity(ComponentName componentName) {
        try {
            Log.d("EEE", "get appInfo:" + componentName.getPackageName());
            return getPackageManager().getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.intent = intent;
        isPaymentReceivedClick = false;//重置
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onInterrupt() {

    }
}

最后,启动辅助功能并调用微信付款码

if(isAccessibilitySettingsOn(MainActivity2.this)){
    Intent intent = new Intent(MainActivity2.this, PayAccessibility.class);
    startService(intent);

    new Handler(Looper.getMainLooper()).postDelayed(() -> {
        Intent intent1 = new Intent();
        ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
        intent1.setAction(Intent.ACTION_MAIN);
        intent1.addCategory(Intent.CATEGORY_LAUNCHER);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent1.setComponent(cmp);
        startActivity(intent1);
    }, 200);
}else {
    startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
    finish();
}

注意:必须得增加辅助功能权限的判断

/**
 * 辅助服务是否开启
 *
 * @param context 活动context
 * @return
 */
public static boolean isAccessibilitySettingsOn(Context context) {
    int accessibilityEnabled = 0;
    final String service = context.getPackageName() + "/" + PayAccessibility.class.getCanonicalName();
    try {
        accessibilityEnabled = Settings.Secure.getInt(context.getApplicationContext().getContentResolver(),
                Settings.Secure.ACCESSIBILITY_ENABLED);
    } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
    }
    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
    if (accessibilityEnabled == 1) {
        String settingValue = Settings.Secure.getString(context.getApplicationContext().getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        if (settingValue != null) {
            mStringColonSplitter.setString(settingValue);
            while (mStringColonSplitter.hasNext()) {
                String accessibilityService = mStringColonSplitter.next();
                if (accessibilityService.equalsIgnoreCase(service)) {
                    return true;
                }
            }
        }
    }
    return false;
}
上一篇:经典蓝牙使用


下一篇:python中对日期对象的处理