Search code examples
androidcalllog

Android pick up entry from call history screen


I have been able to reach the call logs screen of my phone using the below code i got from internet. Is it possible to pick up the entry details, say number, time of call etc of single entry with on click ?

Intent showCallLog = new Intent();
    showCallLog.setAction(Intent.ACTION_VIEW);
    showCallLog.setType(CallLog.Calls.CONTENT_TYPE);
    startActivity(showCallLog);

Solution

  • You can not use a intent to pick a single entry from logs.

    But you can get all info from db and show them into a dialog list and then select any contact you want.

    Check below Code how it works.

    Note: Add permission in manifest and If your OS is 6.0 or greater then get runtime permission from user how here.

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

    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button ContactPickBtn = (Button) findViewById(R.id.btnPick);
            ContactPickBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String[] strFields = {android.provider.CallLog.Calls._ID,
                            android.provider.CallLog.Calls.NUMBER,
                            android.provider.CallLog.Calls.CACHED_NAME,};
                    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
                    // Make you have call logs permissions
                    // if your os is 6.0 get call log permission at runtime.
                    final Cursor cursorCall = getContentResolver().query(
                            android.provider.CallLog.Calls.CONTENT_URI, strFields,
                            null, null, strOrder);
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            MainActivity.this);
                    builder.setTitle("Pick a contact");
                    android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface,
                                            int item) {
                            cursorCall.moveToPosition(item);
                            Toast.makeText(
                                    MainActivity.this,
                                    cursorCall.getString(cursorCall
                                            .getColumnIndex(android.provider.CallLog.Calls.NUMBER)),
                                    Toast.LENGTH_LONG).show();
                            cursorCall.close();
                            return;
                        }
                    };
                    builder.setCursor(cursorCall, listener,
                            android.provider.CallLog.Calls.CACHED_NAME);
                    builder.create().show();
                }
            });
        }
    }