通过OnTouchListener按下按钮时如何打开通话记录?

时间:2018-12-20 12:29:05

标签: android call phone-number calllog

我在add_benchmark( NAME benchmark SOURCES main.cpp ) add_benchmark( NAME benchmark_other SOURCES main.cpp other.cpp ) 的{​​{1}}的{​​{1}}上设置了电话号码,因此,当我按下按钮,电话的呼叫日志框打开并且我为编辑文本框选择最近使用的数字之一。那么,现在OnClickListener的动作要写些什么呢?

我在MainActivity.java中编写的代码为:

drawableRight

1 个答案:

答案 0 :(得分:0)

您必须获得以下权限

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

使用此代码获取通话记录

if (Build.VERSION.SDK_INT >= 23) {
 requestPermissions(new String[] {
  Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
 }, 200);
}

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

while (c.moveToNext()) {
 String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
 String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
 String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
 int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
 Log.e("TEST", num);
}

如果要在列表中获取它

ArrayList < String > list = new ArrayList < > ();

if (Build.VERSION.SDK_INT >= 23) {
 requestPermissions(new String[] {
  Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
 }, 200);
}

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

while (c.moveToNext()) {
 String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
 String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
 String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
 int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
 list.add(num);
}

final String[] listAccounts = list.toArray(new String[0]);
final int[] currentApp = {
 0
};

// Dialog box for multiple card accounts
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
//mBuilder.setTitle("Choose an account :");
mBuilder.setTitle(Html.fromHtml("<font color='#FF7F27'>Num</font>"));
mBuilder.setCancelable(false);
mBuilder.setSingleChoiceItems(listAccounts, 0, new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialogInterface, int i) {
  Log.e("Debug_", "Clicked: " + listAccounts[i]);
  currentApp[0] = i;
 }
});
mBuilder.setPositiveButton("Select", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialogInterface, int which) {
  Log.e("Debug_", "Name: " + listAccounts[currentApp[0]] + " Index: " + currentApp[0]);
  number.setText(listAccounts[currentApp[0]]);
  dialogInterface.dismiss();
 }
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();