编程语言
首页 > 编程语言> > 如何在Android中以编程方式与USSD对话框进行交互

如何在Android中以编程方式与USSD对话框进行交互

作者:互联网

我想使用USSD对话框,该对话框会在拨打* 123#的任何USSD代码后出现,该对话框要求用户输入选项号以执行特定任务,具体取决于SIM卡供应商.我需要与该对话框进行交互,以便以编程方式在该对话框中提供输入.

但是,我可以使用AccessibilityService在拨打任何USSD代码后读取“警报”对话框中出现的USSD响应,并且在Toast中显示响应,如以下代码所示.我还没有找到任何与USSD对话框进行交互的解决方案.

public class UssdService extends AccessibilityService{
    public static String TAG = "USSD";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.d(TAG, "onAccessibilityEvent");
        String text = event.getText().toString();
        if (event.getClassName().equals("android.app.AlertDialog")) {
            Log.d(TAG, text);
            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onInterrupt() {
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.packageNames = new String[]{"com.android.phone"};
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
    }
}

这是清单中的服务声明:

<service android:name=".UssdService"
    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/config_service" />
</service>

解决方法:

为了与USSD对话框进行交互,我使用了以下代码.

我将以下代码用于click事件:

List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("Send");
for (AccessibilityNodeInfo node : list) {
     node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}

我在EditText中将以下代码用于setText.这是当前焦点所在的setText.

AccessibilityNodeInfo nodeInput = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
Bundle bundle = new Bundle();        
bundle.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,pMPIN);
nodeInput.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,bundle);
nodeInput.refresh();

标签:android-alertdialog,sim-card,ussd,accessibilityservice,android
来源: https://codeday.me/bug/20191119/2032286.html