这是我的计划。
在这个Android项目中,有一个xml文件,其中有一个按钮。点击该按钮后,将显示拨号屏幕,这就是我想要的。
这是我试过的代码:
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle abc) {
//super.onCreate(savedInstanceState);
super.onCreate(abc);
HashMap<String, String> item = new HashMap<String, String>();
HashMap<String, String> item2 = new HashMap<String, String>();
HashMap<String, String> item3 = new HashMap<String, String>();
item.put("name", "Taxi For Sure");
item.put("number", "020 6060 1010");
item.put("address", "For Pune City");
item2.put("name", "OLA Cabs");
item2.put("number", "020 3355 3355");
item2.put("address", "For Pune City");
item3.put("name", "Savaari Car");
item3.put("number", "1800 108 1000");
item3.put("address", "For Pune City");
list.add(item);
list.add(item2);
list.add(item3);
String[] columns = new String[]{"name", "number", "address"};
int[] renderTo = new int[]{R.id.name, R.id.number, R.id.address};
ListAdapter listAdapter = new SimpleAdapter(this, list, R.layout.taxi_row, columns, renderTo);
setListAdapter(listAdapter);
public void onClick(View view) {
int id = view.getId();
int pos = (Integer) view.getTag();
String number = list.get(pos).get("number");
if (id == R.id.imageButton_call) {
Intent callIntent = new Intent();
callIntent.setAction(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + number));
startActivity(callIntent);
}
}
答案 0 :(得分:3)
试试这个:
try {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+number));
context.startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Toast.makeText(context, context.getString("Call has failed"), Toast.LENGTH_LONG).show();
}
}
并确保您在AndroidManifest.xml文件中拥有此权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
答案 1 :(得分:1)
btnPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
phone = editPhone.getText().toString();
dail();
}
});
public void dail() {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+phone));
startActivity(callIntent);
}