在实现拨打电话之前要添加权限,在添加权限的时候要注意位置,写在<user-sdk>的下面。不然在有些的机子上可能会出错,如下
uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE" />
添加完权限之后,我自己写了一个方法打电话,需要传进去电话号码,没有返回值
public void callPhone(String str) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + str));
startActivity(intent);
}
在打电话中需要使用到startActivity()所以当前方法要在继承了activity的类中才能实现。
在listView的监听中可以使用第二个参数获得某个控件,然后对其操作(这里的监听只有点击了相应的控件才会被触发)
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
arg1.findViewById(R.id.sendphone).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v.findViewById(R.id.sendphone);
String str = tv.getText().toString();
callPhone(str);
}
});
}
在这里监听了 textView并实现点击之后获得textView的内容然后调用方法传参实现点击item的子控件实现相应的功能。