Clear Call Logs

madSac

Active Member
Licensed User
Hey,
I wanted to clear user's call logs on googling i found something like this
B4X:
getContentResolver().delete(android.provider.CallLog.Calls.CONTENT_URI, null, null)
Can anyone help me in using it in B4A ...i think it can be done using reflection lib but how ?
 

Erel

Administrator
Staff member
Licensed User
Try this:
B4X:
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getContentResolver")
Dim CONTENT_URI As Object = _
   r.GetStaticField("android.provider.CallLog$Calls", "CONTENT_URI")
r.RunMethod4("delete", Array As Object(CONTENT_URI, Null, Null), _
   Array As String("android.net.Uri", "java.lang.String", "[Ljava.lang.String;")
 

madSac

Active Member
Licensed User
Thanks for replying.I did one mistake.The original thread is at

http://stackoverflow.com/questions/11406588/how-many-ways-to-delete-call-history-on-android

B4X:
Uri uri = Uri.parse("content://call_log/calls");

int d  = getContentResolver().delete(uri, null, null);
//d returns no. of records deleted.


int res = Call_logs.this.getContentResolver().delete(android.provider.CallLog.Calls.CONTENT_URI,"_ID = "+ calls_id_list.get(i),null);
    if (res == 1) {
       // Log delete

    } else {
        // Log not Delete

    }
Please provide conversion for this code.Sorry !!

i used below code but i get an error
B4X:
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getContentResolver")
Dim CONTENT_URI As Object =  r.RunStaticMethod("android.net.Uri", "parse", Array As Object("content://call_log/calls"), Array As String("java.lang.String"))
Log(r.RunMethod4("delete", Array As Object(CONTENT_URI, Null, Null),   Array As String("android.net.Uri", "java.lang.String", "java.lang.String")))
i get following error
B4X:
java.lang.NoSuchMethodException: delete
 
Last edited:

madSac

Active Member
Licensed User
Oh ! Sorry....I modified the code and working great now ...Thank You very much ....
 

NeoTechni

Well-Known Member
Licensed User
Try this:
B4X:
Sub ClearLogs
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getContentResolver")
Dim CONTENT_URI As Object = _
   r.GetStaticField("android.provider.CallLog$Calls", "CONTENT_URI")
r.RunMethod4("delete", Array As Object(CONTENT_URI, Null, Null), _
   Array As String("android.net.Uri", "java.lang.String", "[Ljava.lang.String;"))
end sub
I'm using that, and nothing happens. The code after that line is skipped as if the sub is returns
 

NeoTechni

Well-Known Member
Licensed User
I see since then you made a contentresolver library and someone said they ported your code to it, but didnt post it...
 

NeoTechni

Well-Known Member
Licensed User
Got it:
B4X:
Sub ClearCallLogs
   Dim CON As ContentResolver, r As Reflector, CONTENT_URI As Object
   CON.Initialize("")
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   CONTENT_URI = r.GetStaticField("android.provider.CallLog$Calls", "CONTENT_URI")'content://call_log/calls
   CON.Delete(CONTENT_URI, "DURATION >= 0",Null)
   'r.RunMethod4("delete", Array As Object(CONTENT_URI, Null, Null), Array As String("android.net.Uri", "java.lang.String", "[Ljava.lang.String;"))
End Sub
 

Humberto

Active Member
Licensed User
B4X:
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getContentResolver")
Dim CONTENT_URI As Object =  r.RunStaticMethod("android.net.Uri", "parse", Array As Object("content://call_log/calls"), Array As String("java.lang.String"))
Log(r.RunMethod4("delete", Array As Object(CONTENT_URI, Null, Null),   Array As String("android.net.Uri", "java.lang.String", "[Ljava.lang.String;")))
I´m trying to undestand but I can´t.

In this code where I set the CallLog ID to delete just one log ? ( I get the ID from "CallItem" )


In the original code expect an ID

B4X:
int res = Call_logs.this.getContentResolver().delete(android.provider.CallLog.Calls.CONTENT_URI,"_ID = "+ calls_id_list.get(i),null);

I found also a code below but I can´t translate to B4a

B4X:
public void DeleteCallById(String idd) {  
    this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,CallLog.Calls._ID + " = ? ",
            new String[] { String.valueOf(idd) });
    }
 

Humberto

Active Member
Licensed User
Hi I write a solution.
You can delete by ID ou Number and seems to be faster then the library "myfirstLib".
If you need to delete many fields you can do your own code and put a "doevents" to not block the cell


B4X:
    Dim cl As CallLog
    Dim ci As CallItem
    Dim List_Call As List
    Dim xNum As String
    Dim xRet As Int
    Dim xcursor As Cursor
  
    List_Call.Initialize
    List_Call = cl.GetAll(10)
    ci = List_Call.Get(0)
  
   Log ( "ID: " & ci.Id & " - Numero: " & ci.Number)
   Dim CON As ContentResolver, r As Reflector, CONTENT_URI As Object
   CON.Initialize("")
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   CONTENT_URI = r.GetStaticField("android.provider.CallLog$Calls", "CONTENT_URI")'content://call_log/calls
'   xRet = CON.Delete(CONTENT_URI, "_ID=" & ci.id ,Null)
   xNum = ci.Number
   xRet = CON.Delete(CONTENT_URI, "NUMBER= ?"    ,Array As String(xNum))
    Log ( "retorno delete: " & xRet )
 
Last edited:
Top