<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
public class Address extends Activity {
private static final String TAG="App";
ListView listView;
ListAdapter adapter; //声明一个适配器名称
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.addr);
listView=(ListView) findViewById(R.id.list);
//从数据库获取联系人姓名和电话号码
Cursor cur=this.getContentResolver().query(People.CONTENT_URI,null, null,null,null);
adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,new String[]{People.NAME,People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});
//SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 构造函数参数
this.startManagingCursor(cur);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
// TODO Auto-generated method stub
//从指针的封装类中获得选中项的电话号码并拨号
CursorWrapper wrapper=(CursorWrapper)listView.getItemAtPosition(position);//返回值是Object类需要向下转型成CursorWrapper类型
int columnIndex=wrapper.getColumnIndex(People.NUMBER);//返回从0开始的索引,如果列名不存在将返回-1
if(!wrapper.isNull(columnIndex)){
String number=wrapper.getString(columnIndex);
Log.d(TAG,"number="+number);
// //判断电话号码的有效性
if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
//短信发送
//获得发送短信的管理器,使用的是android.telephony.SmsManager
SmsManager smsManager = SmsManager.getDefault();
//使用短信管理器发送短信内容
//参数一为短信接收者
//参数三为短信内容
//其他可以设为null
String msg="分享一个好东西。。。。";
smsManager.sendTextMessage(number, null, msg, null, null);
Toast.makeText(Address.this,"发送成功", Toast.LENGTH_SHORT).show();
//拨号
//Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+ number));
//startActivity(intent);
}}
}
});
}
}