简体   繁体   English

android读取USSD响应

[英]android read USSD response

I'm trying to read USSD response to get Sim balance amount etc, and I'm having issues, I have been reading many questions related to that on Stackoverflow, nothing has worked so far.我正在尝试阅读 USSD 响应以获取 Sim 余额等,但我遇到了问题,我在 Stackoverflow 上阅读了许多与此相关的问题,但到目前为止没有任何效果。 Except for this that came close: Prevent USSD dialog and read USSD response?除了这个接近: 阻止 USSD 对话并读取 USSD 响应? . . by @HenBoy331来自@HenBoy331

But i'm still having issues with it.但我仍然有问题。 My broadcast receiver doesn't get called too.我的广播接收器也没有被调用。 I'm using 4.4.2我正在使用 4.4.2

But it shows nothing.但它什么也没显示。 I can't seem to parse the message and get the balance.我似乎无法解析消息并获得余额。

I have a MainActivity to make the phone call, ReceiverActivity to implement broadcast receiver, USSDService class to get the USSD response.我有一个MainActivity来打电话, ReceiverActivity来实现广播接收器, USSDService类来获取 USSD 响应。

MainActivity.java主活动.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, USSDService.class));
        dailNumber("100");

    }

    private void dailNumber(String code) {
        String ussdCode = "*" + code + Uri.encode("#");
        startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));
    }
}

RecieverActivity.java接收者活动.java

public class RecieverActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter("com.times.ussd.action.REFRESH");
        this.registerReceiver(new Receiver(), filter);
    }

    public class Receiver extends BroadcastReceiver {

        private String TAG = "XXXX";

        @Override
        public void onReceive(Context context, Intent intent) {

            String message = intent.getStringExtra("message");
            Log.i(TAG, "Got message: " + message);

        }

    }

}

USSDService.java USSDService.java

public class USSDService extends AccessibilityService {

        public String TAG = "XXXX";

        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            Log.d(TAG, "onAccessibilityEvent");

            AccessibilityNodeInfo source = event.getSource();
        /* if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && !event.getClassName().equals("android.app.AlertDialog")) { // android.app.AlertDialog is the standard but not for all phones  */
            if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && !String.valueOf(event.getClassName()).contains("AlertDialog")) {
                return;
            }
            if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && (source == null || !source.getClassName().equals("android.widget.TextView"))) {
                return;
            }
            if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && TextUtils.isEmpty(source.getText())) {
                return;
            }

            List<CharSequence> eventText;

            if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                eventText = event.getText();
            } else {
                eventText = Collections.singletonList(source.getText());
            }

            String text = processUSSDText(eventText);

            if( TextUtils.isEmpty(text) ) return;

            // Close dialog
            performGlobalAction(GLOBAL_ACTION_BACK); // This works on 4.1+ only

            Log.d(TAG, text);

            // Handle USSD response here
            Intent intent = new Intent("com.times.ussd.action.REFRESH");
            intent.putExtra("message", text);
            sendBroadcast(intent);

        }

        private String processUSSDText(List<CharSequence> eventText) {
            for (CharSequence s : eventText) {
                String text = String.valueOf(s);
                // Return text if text is the expected ussd response
                if( true ) {
                    return text;
                }
            }
            return null;
        }

        @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 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
            info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
            setServiceInfo(info);
        }
    }

AndroidManifest安卓清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dialussd">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service
            android:name=".services.USSDService"
            android:enabled="true"
            android:exported="true"
            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>

        <receiver android:name=".RecieverActivity$Receiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>



    </application>

</manifest>

Please, is there any way i'm implementing this wrongly, Or perhaps there is a new way to get USSD responses which works?拜托,有什么办法我错误地实施了这个,或者也许有一种新的方法来获得有效的 USSD 响应?

After the launch, change the settings manually启动后,手动更改设置

Setting->Accessibility Setting -> You can see a option 'your app name'.设置->辅助功能设置->您可以看到“您的应用程序名称”选项。 Turn it on.打开它。 (This has to be done from as a part of application flow(not manual)) (这必须作为应用程序流程的一部分来完成(非手动))

instead of using broadcast receiver use these two lines of code in而不是使用广播接收器使用这两行代码

add this code in MainActivity在 MainActivity 中添加此代码

public static void setTextViewToModify(String  Text) {
        textView.setText(Text);}

and add this in service class with in onAccessibityEvent并将其添加到服务类中的 onAccessibityEvent

MainActivity.setTextViewToModify(text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM