Android Question How to use BlockedNumberContract & BlockedNumberContract.BlockedNumbers ?

palmzac

Active Member
Licensed User
Longtime User
Hi,

I want to use these API to handle 'Block incoming-call' in my app. How to do it ? Thanks !
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Phone library is sufficient

Hi,

I want to use these API to handle 'Block incoming-call' in my app. How to do it ? Thanks !
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Hi Star-Dust,
may not be able
What do you mean?

Have you tried that?
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim PE As PhoneEvents
    Dim PhoneId As PhoneId
End Sub

Sub Service_Create
    PE.InitializeWithPhoneState("PE", PhoneId)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent1 As Intent)
      
    If State="RINGING" Then
        'Squillo
        if IncomingNumber ="0039333xxxx" then KillCall 
    Else If State="OFFHOOK" Then
        ' OFF Hook
    else If State="IDLE" Then
        'IDEL
      
    End If
End Sub

Sub KillCall
   Dim r As Reflector
   r.Target = r.GetContext
   Dim TelephonyManager, TelephonyInterface As Object
   TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
   r.Target = TelephonyManager
   TelephonyInterface = r.RunMethod("getITelephony")
   r.Target = TelephonyInterface
   r.RunMethod("endCall")
End Sub

Update: For newer versions of android (12+) there is another method (TelecomManager rather TelephonyManager)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you can try this Lib. It is a short and incomplete wrap.
Most of the methods are not working for me as my app is not the default sms or default dialer app. Only these apps are allowed to block, unblock

Even querying the list is not allowed. I always run into this exception

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
canCurrentUserBlockNumbers = true
main_activity_create (java line: 339)
java.lang.SecurityException: Caller must be system, default dialer or default SMS app
at android.os.Parcel.readException(Parcel.java:1704)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:538)
at android.content.ContentResolver.query(ContentResolver.java:480)
at de.donmanfred.BlockedNumberContractwrapper.query(BlockedNumberContractwrapper.java:45)
at b4a.example.main._activity_create(main.java:339)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

But try youself...
 

Attachments

  • BlockedNumberContractV0.1.zip
    2 KB · Views: 402
  • bncEx.zip
    6.9 KB · Views: 377
Last edited:
Upvote 0

palmzac

Active Member
Licensed User
Longtime User
Hi Star-Dust,

Thanks for your help !

Yes. It is my solution currently. But, It may be change on the next android version.

https://android-developers.googleblog.com/2018/02/improving-stability-by-reducing-usage.html?m=1

https://www.xda-developers.com/google-undocumented-hidden-apis-android-p/

What do you mean?

Have you tried that?
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim PE As PhoneEvents
    Dim PhoneId As PhoneId
End Sub

Sub Service_Create
    PE.InitializeWithPhoneState("PE", PhoneId)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent1 As Intent)
       
    If State="RINGING" Then
        'Squillo
        if IncomingNumber ="0039333xxxx" then KillCall  
    Else If State="OFFHOOK" Then
        ' OFF Hook
    else If State="IDLE" Then
        'IDEL
       
    End If
End Sub

Sub KillCall
   Dim r As Reflector
   r.Target = r.GetContext
   Dim TelephonyManager, TelephonyInterface As Object
   TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
   r.Target = TelephonyManager
   TelephonyInterface = r.RunMethod("getITelephony")
   r.Target = TelephonyInterface
   r.RunMethod("endCall")
End Sub
 
Upvote 0

palmzac

Active Member
Licensed User
Longtime User
Hi DonManfred,

Thank you very much !

I will try it. ^^

[
QUOTE="DonManfred, post: 569920, member: 42649"]you can try this Lib. It is a short and incomplete wrap.
Most of the methods are not working for me as my app is not the default sms or default dialer app. Only these apps are allowed to block, unblock

Even querying the list is not allowed. I always run into this exception



But try youself...[/QUOTE]
 
Upvote 0
Top