1

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);
  • check this link..http://stackoverflow.com/questions/19493360/get-last-call-duration-in-android – dipali Aug 01 '16 at 05:24

1 Answers1

3

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();
            }
        });
    }
}
Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • Thanks @Sohail Zahid .......... It was of much help.. though this is not what i wanted completely...a little modification would serve my purpose...but yes it was a guide in the right direction...thank you again – bharatkumar Aug 01 '16 at 06:45
  • @Sohail Zahid can you help to get call log pic without using contact cursor,because with extra cursor its taking time. – Sagar Oct 07 '17 at 07:12
  • Thanks, @Sohail, it worked for me. Is there a way I can display a phone number as a label for unsaved contacts – Enock Lubowa Jan 03 '20 at 21:18