Android Question intent phone call how to

asap74

Member
Licensed User
hi you all,
i've to do a call, i use the following code:

B4X:
                    Dim intent1 As Intent
                    intent1.Initialize("android.intent.action.CALL", "tel:999")
                    StartActivity(intent1)


on my phone it works on another smartphone it doesnt work.

it is the right code to do a call?
I mean, the olny thing i ve to think is that i use an old code and maybe there is a better code to do a call.
Hoping to have explained the problem.
thank you.
 

JohnC

Expert
Licensed User
Longtime User
That should work - maybe the number format needs to be fixed (like adding a country code and a "+" to it.

Also, make sure you have this permission in the manifest:
B4X:
AddPermission(android.permission.CALL_PHONE)
 
Upvote 0

YPMN

Member
Licensed User
Longtime User
hi, because i have the same problem, i would like to clarify that on android version lower than 10, the code worked fine for me while it fails from android 10; this, despite the permission added in the manifesto as advised by JohnC. Is there anything else I forgot to add to make the call successful?
 
Upvote 0

YPMN

Member
Licensed User
Longtime User
Here is a snippet of error message I get in a B4XPage project (despite permission in manifest)

B4X:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx flg=0x20000 cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{603453d 11401:zns.callintent/u0a422} (pid=11401, uid=10422) with revoked permission android.permission.CALL_PHONE
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I think the key to the problem is why was the permission "revoked permission android.permission.CALL_PHONE"?

Make sure the user's "Phone" permissions are not blocking the app from making calls.
 
Upvote 0

YPMN

Member
Licensed User
Longtime User
Please! Here is my code

Manifest:

Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.CALL_PHONE)
'End of default text.

B4XMainPage:

B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    
    Dim intent1 As Intent
    intent1.Initialize("android.intent.action.CALL", "tel:089XXXXXXX")
    StartActivity(intent1)
    
    Log("call")
    
End Sub
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
Please! Here is my code

Manifest:

Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.CALL_PHONE)
'End of default text.

B4XMainPage:

B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
  
    Dim intent1 As Intent
    intent1.Initialize("android.intent.action.CALL", "tel:089XXXXXXX")
    StartActivity(intent1)
  
    Log("call")
  
End Sub
Later versions of Android you can't just add the permissions in the Manifest, you need to get specific permission from the user too.

Try this (inB4XPage_Created)

B4X:
        Dim rplist As List
        Dim rpPermission     As String
        rplist.Initialize
        rplist.AddAll(Array As String(rp.PERMISSION_CALL_PHONE))  
        Do While rplist.Size > 0
            rpPermission = rplist.Get(0)

            rp.CheckAndRequest(rpPermission)
                   
            wait for B4XPage_PermissionResult(rpPermission As String, Result As Boolean)
                   
            If  Result = True Then
                rplist.RemoveAt(0)
                Continue
            End If
                   
            If  Result = False Then
                ExitApplication
            End If
        Loop
 
Upvote 0

YPMN

Member
Licensed User
Longtime User
Sorry, I finally figured out that it was a permissions configuration problem on my phone (probably because the apk installs outside of playstore). On first run it crashed and went to grant call permissions at the settings level... then everything worked. JohnC, thanks for the assistance.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Later versions of Android you can't just add the permissions in the Manifest, you need to get specific permission from the user too.

Try this (inB4XPage_Created)

B4X:
        Dim rplist As List
        Dim rpPermission     As String
        rplist.Initialize
        rplist.AddAll(Array As String(rp.PERMISSION_CALL_PHONE)) 
        Do While rplist.Size > 0
            rpPermission = rplist.Get(0)

            rp.CheckAndRequest(rpPermission)
                  
            wait for B4XPage_PermissionResult(rpPermission As String, Result As Boolean)
                  
            If  Result = True Then
                rplist.RemoveAt(0)
                Continue
            End If
                  
            If  Result = False Then
                ExitApplication
            End If
        Loop
Hello,
I had the same problem and using your code it works well because it allows to offer authorization to use the phone for the application.
Question: Where should I ideally place this code, when the main page loads, or in Starter. If in Starter how to split the code.
THANKS
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top