运行 Android 中的 USSD 代码并将应用程序保留在第一位

Run USSD Code in Android and Keep the App in the firstground

我正在 Android 中创建一个应用程序,后台需要 运行 USSD 代码。没有在后台发送我的申请,

每当我使用 Intent.ACTION_CALL 到 运行 USSD

String ussdCode = "*" + "123" + Uri.encode("#");
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));

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

因此可以运行 USSD代码无需打开前面的拨号接口。

谢谢。

使用以下代码:

String ussdCode = "*123#";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(ussdToCallableUri(ussdCode));
try{
    startActivity(intent);
} catch (SecurityException e){
    e.printStackTrace();
}

这里是将您的 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);
}