查看联系人

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();
        }
        
        
    }
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值