Tuesday 30 November 2021

Delete phone call logs programmatically

 
Here we are going to explain how we can delete all phone call logs programmatically


Include the READ_CALL_LOG and WRITE_CALL_LOG permissions in your manifest file,
 
  <uses-permission android:name="android.permission.READ_CALL_LOG"/>
  <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>



Here is the function to delete all the phone calls, specify a  TYPE for the calls so that it gets removed,

private void deletePhoneCallLogs() {
    Cursor cursor = getContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) {
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(CallLog.Calls._ID));
        Uri deleteUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI, id);
        Log.d("PhoneCall","My delete PhoneCall"+deleteUri);
        getContext().getContentResolver().delete(CallLog.Calls.CONTENT_URI , "type="+CallLog.Calls.TYPE , null);        }
    cursor.close();
}



Here is the type of the call (incoming, outgoing or missed).
INCOMING_TYPE
OUTGOING_TYPE
MISSED_TYPE
VOICEMAIL_TYPE
REJECTED_TYPE
BLOCKED_TYPE
ANSWERED_EXTERNALLY_TYPE



For delete the missed calls here is the code,

getContentResolver().delete(CallLog.CONTENT_URI , "type="+CallLog.Calls.MISSED_TYPE , null);




You can get more information about Call Types from here  https://developer.android.com/reference/android/provider/CallLog.Calls#TYPE




Monday 29 November 2021

Context on Android

 Here we are going to explain what's exactly is a Context class in Android and what is it used for?

As the name suggests, it's the context of the current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application).

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).
Typical uses of context:

  • Creating new objects : Creating new views, adapters, listeners:
 TextView tv = new TextView(getContext());
 ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);

  • Accessing standard common resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
 context.getSystemService(LAYOUT_INFLATER_SERVICE)
 getApplicationContext().getSharedPreferences(*name*, *mode*);


  • Accessing components implicitly: Regarding content providers, broadcasts, intent
 getApplicationContext().getContentResolver().query(uri, ...);


You can read more from the Developer Site, From there you can understand it more clearly.