Android中管理USSD对话框的新方法

31

我已经在这里和其他论坛上阅读了几天关于USSD对话框的帖子。 (通过USSD,我指的是运营商通知中包含呼叫成本详细信息的内容)。

我看到了许多解决方案,它们显然适用于Android中较低的API级别,但经过了很多测试,发现它们不再起作用,或者至少我无法使它们正常工作。

首先,我想知道是否有任何方法可以检测到用户是否显示了USSD对话框。因此,我尝试了这个: Prevent USSD dialog and read USSD response?

但是,从日志中我得到的所有信息都与我的应用程序相关,虽然我可以在Eclipse的LogCat中看到它们,但没有捕获到与MMI相关的日志! 也许它只适用于比我的Android版本更低的版本(4.2.2)。

然后,我决定使用"IExtendedNetworkService",就像以下链接和许多其他链接中使用的方式一样:

https://github.com/alaasalman/ussdinterceptor

Using IExtendedNetworkService to get USSD response in Android

How to read USSD messages in android?

Implementing USSD features. Binding a service to the PhoneUtils without restarting the phone on every update

但是,它对于Android 4.2.2及以上版本也是无用的。

然后我发现了这个链接: How to dismiss system dialog in Android?

看起来很有前途,但即使经过了许多测试,我也无法让它正常工作。

我以为可能是我做错了什么,或者它只能用在更低版本的API上。

之后,我尝试了许多其他方法,如模拟Home键和返回键,甚至通过编程方式触摸屏幕以隐藏该通知,但由于它们都在我的活动下运行,所以都没有起作用。

是否有人知道这些方法或其他方法是否适用于管理甚至检测Android 4.2.2及以上版本中的USSD消息?

非常感谢任何帮助,提前致谢。


2
有人了解这方面的最新情况吗? - Lord Sepid
我没有发现任何新的东西。我按照你在这里概述的完全相同的路径走了下去。自那时以来,你有找到解决方案吗? - Arthur Hylton
没有,什么有用的都没有 :'( - Lord Sepid
1
你好,你有找到任何解决方案吗? - Harry Sharma
没有办法做到这一点。Android不提供USSD API。任何这些黑客都不受支持,可能随时会出现问题。最好找到完全不使用USSD的不同方法来完成它。实际上,USSD在一般情况下应该被视为死亡-它是在手机没有数据时的一个黑客,不应该在现代世界中使用。 - Gabe Sechan
这个问题有最新的进展吗? - Michael
2个回答

8
可以使用无障碍服务实现。 首先创建一个服务类:
public class USSDService extends AccessibilityService {

public static String TAG = USSDService.class.getSimpleName();

@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

}

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

在Android清单文件中声明。
<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/ussd_service" />

创建一个名为 ussd_service 的可访问性服务的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description"
android:notificationTimeout="0"
android:packageNames="com.android.phone" />

就是这样,应用程序安装完成后,您必须在可访问性设置中启用服务(设置 -> 可访问性设置 -> 您的应用程序名称)。

解决方案详见此处此处(俄语)


这应该是一个注释 - ρяσѕρєя K
@HenBoy331,谢谢。我会尽快检查并告诉你它是否适用于我。 - Lord Sepid
1
嗨,我已经尝试过这个了,在我的应用程序中成功获取了USSD响应,但问题是,USSD对话框无法关闭,我正在使用Lolipop。 - dmh
我使用的是Marshmallow系统,performGlobalAction无法关闭USSD对话框。看起来唯一的方法是通过编程点击USSD对话框按钮。 - tsil

0

很遗憾,没有办法。

我们一直在寻找那个命令,也在寻找解决这个问题的方法,但如果只通过手机日志的解析来获取信息,这并不能保证在所有设备上都能100%工作。

因此,我们与我们国家的移动运营商(我们只有3个移动运营商)达成了协议,并提供了API来获取电话号码的余额,但这种方法只在本地有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接