public class Seconde extends Activity {
private ListView listV;
private ContentResolver resolver;
private ArrayList<Bean> list;
private String json;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
listV.setAdapter(new ArrayAdapter<Bean>(Seconde.this,
android.R.layout.simple_list_item_1, list));
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aontact);
listV = (ListView) findViewById(R.id.listV);
}
public void onPlays(View v) {
new Thread(){
public void run() {
ArrayList<Bean> onPhone = onPhone();
Gson gson = new Gson();
json = gson.toJson(onPhone);
onSD(json);
System.out.println("Gone解析:"+json.toString());
};
}.start();
}
private ArrayList<Bean> onPhone() {
try {
Log.i("-------", "onPhone");
list = new ArrayList<Bean>();
resolver = getContentResolver();
Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri data_uri = Uri.parse("content://com.android.contacts/data");
Cursor cursor_raw = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null);
while (cursor_raw.moveToNext()) {
Bean bean = new Bean();
String contact_id = cursor_raw.getString(cursor_raw.getColumnIndex("contact_id"));
Cursor cursor_data = resolver.query(data_uri, new String[]{"data1","mimetype"},
"raw_contact_id = ? ", new String[]{contact_id}, null);
while (cursor_data.moveToNext()) {
String data1 = cursor_data.getString(cursor_data.getColumnIndex("data1"));
String mimetype = cursor_data.getString(cursor_data.getColumnIndex("mimetype"));
if ("vnd.android.cursor.item/name".equals(mimetype)) {
bean.setName(data1);
}else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
bean.setPhone(data1);
}else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) {
bean.setEmail(data1);
}
}
list.add(bean);
Log.i("------->", "haha"+list.toString());
}
handler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void onSD(String json){
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String file = Environment.getExternalStorageDirectory()+File.separator+"haha.txt";
File file2 = new File(file);
FileOutputStream out = new FileOutputStream(file2);
for (int i = 0; i < list.size(); i++) {
/*out.write(list.get(i).getName().getBytes());
out.write(list.get(i).getEmail().getBytes());
out.write(list.get(i).getPhone().getBytes());*/
out.write(json.getBytes());
out.flush();
}
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private ListView listV;
private ContentResolver resolver;
private ArrayList<Bean> list;
private String json;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
listV.setAdapter(new ArrayAdapter<Bean>(Seconde.this,
android.R.layout.simple_list_item_1, list));
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aontact);
listV = (ListView) findViewById(R.id.listV);
}
public void onPlays(View v) {
new Thread(){
public void run() {
ArrayList<Bean> onPhone = onPhone();
Gson gson = new Gson();
json = gson.toJson(onPhone);
onSD(json);
System.out.println("Gone解析:"+json.toString());
};
}.start();
}
private ArrayList<Bean> onPhone() {
try {
Log.i("-------", "onPhone");
list = new ArrayList<Bean>();
resolver = getContentResolver();
Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri data_uri = Uri.parse("content://com.android.contacts/data");
Cursor cursor_raw = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null);
while (cursor_raw.moveToNext()) {
Bean bean = new Bean();
String contact_id = cursor_raw.getString(cursor_raw.getColumnIndex("contact_id"));
Cursor cursor_data = resolver.query(data_uri, new String[]{"data1","mimetype"},
"raw_contact_id = ? ", new String[]{contact_id}, null);
while (cursor_data.moveToNext()) {
String data1 = cursor_data.getString(cursor_data.getColumnIndex("data1"));
String mimetype = cursor_data.getString(cursor_data.getColumnIndex("mimetype"));
if ("vnd.android.cursor.item/name".equals(mimetype)) {
bean.setName(data1);
}else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
bean.setPhone(data1);
}else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) {
bean.setEmail(data1);
}
}
list.add(bean);
Log.i("------->", "haha"+list.toString());
}
handler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void onSD(String json){
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String file = Environment.getExternalStorageDirectory()+File.separator+"haha.txt";
File file2 = new File(file);
FileOutputStream out = new FileOutputStream(file2);
for (int i = 0; i < list.size(); i++) {
/*out.write(list.get(i).getName().getBytes());
out.write(list.get(i).getEmail().getBytes());
out.write(list.get(i).getPhone().getBytes());*/
out.write(json.getBytes());
out.flush();
}
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}