Android pick up entry from call history screen

為{幸葍}努か 提交于 2020-01-30 04:03:12

问题


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);

回答1:


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();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/38691524/android-pick-up-entry-from-call-history-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!