Android从通话记录屏幕中选择条目

时间:2016-08-01 05:23:11

标签: android calllog

我已经能够使用以下从互联网获得的代码到达我手机的通话记录屏幕。是否可以通过点击获取单个条目的条目详细信息,例如号码,通话时间等?

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

1 个答案:

答案 0 :(得分:2)

您无法使用意图从日志中选择单个条目。

但您可以从数据库获取所有信息并将其显示在dialog list中,然后选择您想要的任何contact

检查以下代码的工作原理。

注意:在清单中添加权限,如果您的OS is 6.0 or greater然后从用户那里获得runtime permission 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();
            }
        });
    }
}