在Android中运行USSD代码并保持应用程序在前台

8
我正在创建一款Android应用程序,需要在后台运行USSD代码,而不让我的应用程序在后台发送。 每当我使用Intent.ACTION_CALL来运行USSD时。
String ussdCode = "*" + "123" + Uri.encode("#");
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));

我的应用程序被发送到后台,并在我的应用程序上打开拨号界面。

因此,可以在不打开拨号界面的情况下运行USSD代码。

谢谢。


与此相同的问题:https://dev59.com/O1XTa4cB1Zd3GeqP3Z9O - Plugie
1个回答

3
使用以下代码:
String ussdCode = "*123#";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(ussdToCallableUri(ussdCode));
try{
    startActivity(intent);
} catch (SecurityException e){
    e.printStackTrace();
}

以下是将您的ussd代码转换为可调用ussd的方法:
以下是将您的ussd代码转换为可调用ussd的方法:
private Uri ussdToCallableUri(String ussd) {

    String uriString = "";

    if(!ussd.startsWith("tel:"))
        uriString += "tel:";

    for(char c : ussd.toCharArray()) {

        if(c == '#')
            uriString += Uri.encode("#");
        else
            uriString += c;
    }

    return Uri.parse(uriString);
}

13
我不明白为什么这个答案被采纳。你的代码与问题中原始贴出的代码相同,只是更长了一些行数。 - Mr.D
而且你本来可以使用字符串构建器或者甚至是 String.replace("#", Uri.encore("#")) 来代替。 - Lorenzo

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