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

问题描述:

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

I want to use USSD dialog which comes after dialing any USSD code say *123# which asks user to enter option number to perform specific task(s) depending upon sim card vendors. I need to interact with that dialog to provide input in the text box given into it programmatically.

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

However, I am able to read the USSD response that comes in Alert Dialog after dialing any USSD code, using AccessibilityService and I'm showing the response in a Toast as shown in the code below. I haven't found any solution to interact with USSD dialog yet.

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);
    }
}

以下是清单中的服务声明:

Here is the service declaration in Manifest:

<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对话框进行交互,我使用了以下代码.

For interacting with USSD dialog, I used below code.

我将以下代码用于点击事件:

I used the below code for click event:

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

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

I used the below code for setText in EditText. This is setText where the current focus is.

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();