deathnote
deathnote

Reputation: 95

Displaying a particular contact from the call log using intent

I'm trying to display a number from call log. I've got a button in my app which will redirect user to call log activity of phone and when user selects a contact it will be displayed in the textview of the main activity. I've added permission in manifest for call log read but i'm getting this error:

08-17 16:54:17.113: E/AndroidRuntime(18121): FATAL EXCEPTION: main
    08-17 16:54:17.113: E/AndroidRuntime(18121): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://call_log/calls }
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.app.Activity.startActivityForResult(Activity.java:3388)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.app.Activity.startActivityForResult(Activity.java:3349)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at com.example.callogdemo.MainActivity$1.onClick(MainActivity.java:32)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.view.View.performClick(View.java:4211)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.view.View$PerformClick.run(View.java:17446)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.os.Handler.handleCallback(Handler.java:725)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.os.Handler.dispatchMessage(Handler.java:92)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.os.Looper.loop(Looper.java:153)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at android.app.ActivityThread.main(ActivityThread.java:5297)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at java.lang.reflect.Method.invokeNative(Native Method)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at java.lang.reflect.Method.invoke(Method.java:511)
    08-17 16:54:17.113: E/AndroidRuntime(18121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)

Code:

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setData(android.provider.CallLog.Calls.CONTENT_URI);
                    startActivityForResult(intent, 0);
                }
            });



    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        String[] callLogFields = { android.provider.CallLog.Calls._ID,
                android.provider.CallLog.Calls.NUMBER,
                android.provider.CallLog.Calls.CACHED_NAME };
        String phoneNumber="Not Selected Yet" ;
        Cursor c =  managedQuery(intent.getData(), callLogFields, null, null, null);
        if(c!=null && c.moveToFirst())
        {
    phoneNumber=c.getString(c.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        }
        c.close();
        textview.setText(phoneNumber);
    }

Upvotes: 2

Views: 783

Answers (1)

Simon Marquis
Simon Marquis

Reputation: 7526

It's because you simply can't.
Android doesn't provide a way to ACTION_PICK a call log.
But you can use this workaround to display the list on your app.

        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";
        final Cursor cursorCall = getActivity().getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, strFields, null, null,
                strOrder);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Select recent contact");
        android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int item) {
                cursorCall.moveToPosition(item);
                Toast.makeText(getActivity(), 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();

This will requires the following permission :

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

Upvotes: 1

Related Questions