android读取USSD响应

时间:2016-11-10 12:54:08

标签: java android ussd

我试图阅读USSD响应以获得Sim余额等,而且我遇到了问题,我一直在阅读与Stackoverflow相关的许多问题,到目前为止还没有任何工作。除此之外:Prevent USSD dialog and read USSD response?。来自@HenBoy331

但我仍然遇到问题。我的广播接收器也没有被调用。我使用的是4.4.2

但它没有显示任何内容。我似乎无法解析消息并获得平衡。

我有MainActivity拨打电话,ReceiverActivity实施广播接收器,USSDService课程以获得USSD回复。

MainActivity.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

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

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>

拜托,我有没有办法错误地实现这一点,或者有一种新方法可以获得有效的USSD回复?

1 个答案:

答案 0 :(得分:0)

启动后,手动更改设置

设置 - &gt;辅助功能设置 - &gt;您可以看到“您的应用名称”选项。打开它。 (这必须作为应用程序流程的一部分(而不是手动))