简体   繁体   English

Android Studio-在拨号盘中执行USSD代码

[英]Android Studio - Execute USSD code in Dial Pad

I have a problem right now with my code, i dont know if anybody can help me. 我的代码现在有问题,我不知道是否有人可以帮助我。 I need to run a USSD code in my dial pad, and i'm using this code: 我需要在拨号盘中运行USSD代码,并且正在使用以下代码:

Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:"+  Uri.encode("##873283#")));

startActivity(i);

So that open the dial pad and doesn't do anything. 这样就可以打开拨号盘,并且什么也不做。 If i insert that manually, the USSD code is run correctly. 如果我手动插入,USSD代码将正确运行。

Beware this USSD code, executes without the call button. 当心此USSD代码,无需调用按钮即可执行。

You can check this video where i'm running the code manually: Video 1 您可以在我手动运行代码的地方查看此视频: 视频1

Running the code from the app (it will receives a command 'update' and execute the code i posted): Video 2 从应用程序运行代码(它将收到命令“ update”并执行我发布的代码): 视频2

I think that code is only executed if we have keypress's. 我认为只有在有按键的情况下才执行代码。 Its that the problem? 这是问题吗? There is a way i can simulate that? 有一种方法可以模拟吗?

Thank you so much 非常感谢

If you want to initiate the call directly without user's interaction , you can use action Intent.ACTION_CALL. 如果要在没有用户交互的情况下直接发起呼叫,则可以使用操作Intent.ACTION_CALL。 Due to security on the latest SDKs, you initially have to ask for some permissions: 由于最新SDK的安全性,您最初必须要求一些权限:

private void dialNumber(final String phoneNumber) {
    //permission is automatically granted on sdk<23 upon installation
    if (Build.VERSION.SDK_INT >= 23) {
        //Check if you have permissions to execute a call (number or USSD)
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG","Permission is granted");
            startActivity(new Intent(ACTION_CALL, Uri.fromParts("tel", phoneNumber, null)));

        } else {
            //Request permissions if we don't have them
            Log.v("TAG","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CALL_PHONE}, 1);
            startActivity(new Intent(ACTION_CALL, Uri.fromParts("tel", phoneNumber, null)));
        }
    }
    else {
        Log.v("TAG","Permission is granted");
        startActivity(new Intent(ACTION_CALL, Uri.fromParts("tel", phoneNumber, null)));
    }

}

Add the following permission (before application tag) in your AndroidManifest.xml: 在您的AndroidManifest.xml中添加以下权限(在应用程序标记之前):

<uses-permission android:name="android.permission.CALL_PHONE" />

Call the method when you need to use it and pass in a phone number as string: 在需要使用该方法时调用该方法,并将电话号码作为字符串传递:

dialNumber("##323232#");

OR 要么

dialNumber(123456789.toString());

Video I just took now here 我现在 在这里 拍摄的视频

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

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