简体   繁体   English

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

[英]Run USSD Code in Android and Keep the App in the firstground

I am creating a App in Android, which required run USSD Code in background. 我正在Android中创建一个应用程序,需要在后台运行USSD代码。 without send my application in background, 没有在后台发送我的申请,

Whenever I am using Intent.ACTION_CALL to run USSD 每当我使用Intent.ACTION_CALL运行USSD时

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

it send my application in background, and open dialer Interface on my application. 它在后台发送我的应用程序,并在我的应用程序上打开拨号程序接口。

So it is possible to run USSD code without open Dialer Interface in front. 因此,可以在没有打开拨号器接口的情况下运行USSD代码。

Thanks. 谢谢。

Use following code: 使用以下代码:

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

Here is the method to convert your ussd code to callable 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);
}

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

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