通过 OnTouchListener 按下按钮时如何打开通话记录?
How to open Call Log when button pressed through OnTouchListener?
我已经在 edittext
中的 drawableRight
上设置了 phone 个号码,所以,当我按下按钮时,你能帮我打开通话记录吗,我的 phone 的通话记录框打开,我 select 编辑文本框最近使用的号码之一。那么,现在要为 onclicklistener
的操作写些什么?
我在 MainActivity.java 中编写的代码为:
public class MainActivity extends AppCompatActivity {
CountryCodePicker ccp;
TextInputEditText number;
@SuppressLint({"ResourceType", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ccp = findViewById(R.id.ccp);
number = (TextInputEditText) findViewById(R.id.Phone_Number);
number.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (number.getRight() - number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
CallsProvider callsProvider = new CallsProvider (getApplicationContext());
callsProvider.getCalls();
}
}
return false;
}
});
}
}
您必须获得如下权限
<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();
我已经在 edittext
中的 drawableRight
上设置了 phone 个号码,所以,当我按下按钮时,你能帮我打开通话记录吗,我的 phone 的通话记录框打开,我 select 编辑文本框最近使用的号码之一。那么,现在要为 onclicklistener
的操作写些什么?
我在 MainActivity.java 中编写的代码为:
public class MainActivity extends AppCompatActivity {
CountryCodePicker ccp;
TextInputEditText number;
@SuppressLint({"ResourceType", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ccp = findViewById(R.id.ccp);
number = (TextInputEditText) findViewById(R.id.Phone_Number);
number.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (number.getRight() - number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
CallsProvider callsProvider = new CallsProvider (getApplicationContext());
callsProvider.getCalls();
}
}
return false;
}
});
}
}
您必须获得如下权限
<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();