仅导出手机联系人
package com.example.test_tx;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private String getQueryData(){
ContentResolver resolver=this.getContentResolver();
String[] columns = new String[] {Phone.DISPLAY_NAME,Phone.NUMBER};
Cursor cursor = resolver.query(Phone.CONTENT_URI, columns, null,null,"sort_key");
StringBuilder sb=new StringBuilder();
while (cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
if(TextUtils.isEmpty(number)){
continue;
}
sb.append(name).append("\n"). append("联系方式").append(number).append("\n").append("---------------------------------------------").append("\n");
}
cursor.close();
return sb.toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(getQueryData());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
导出手机联系人,并且实现拨号和发信息的功能
package com.example.lxrddh;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView=(ListView)findViewById(R.id.listView);
Cursor cur=getContentResolver().query(Phone.CONTENT_URI,null, null, null, "sort_key");
startManagingCursor(cur);
final ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,cur,new String[]{
Phone.DISPLAY_NAME,Phone.NUMBER},
new int[]{android.R.id.text1,android.R.id.text2});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
CursorWrapper wrapper=(CursorWrapper)listView.getItemAtPosition(arg2);
int columnIndex=wrapper.getColumnIndex(Phone.NUMBER);
if(!wrapper.isNull(columnIndex)){
final String number=wrapper.getString(columnIndex);
if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择");
builder.setPositiveButton("通话",new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
startActivity( new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+ number)));
}
});
builder.setNegativeButton("短信",new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity (new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+ number)));
}
});
builder.create().show();
}
}
}
});
}
private void openToast(String str){
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS"/>