RxJava下使用ContentResolve获取系统通讯录

这里获取通讯录的方式有问题,每获取一个联系人就要切换一次线程,求修改方法

public class MainActivity
		extends AppCompatActivity {

	private Context mContext;

	private String TAG = "TAG";

	ArrayList<ContactBean> contacts = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		mContext = MainActivity.this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		doAction();
	}

	private void doAction() {

		Observable.create(new Observable.OnSubscribe<Cursor>() {

			@Override
			public void call(Subscriber<? super Cursor> subscriber) {

				ContentResolver contentResolver = mContext.getContentResolver();
				Cursor cursor = contentResolver.query(
						ContactsContract.RawContacts.CONTENT_URI,
						null,
						null,
						null,
						null
													 );

				while (cursor.moveToNext()) {
					subscriber.onNext(cursor);
				}
				cursor.close();
				subscriber.onCompleted();
			}
		})
				  .map(new Func1<Cursor, ContactBean>() {

					  @Override
					  public ContactBean call(Cursor cursor) {

						  ContactBean contactBean = new ContactBean();
						  String      name        = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
						  contactBean.setName(name);
						  //获取联系人号码
						  String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
						  Cursor phoneCursor = mContext.getContentResolver()
													   .query(
															   android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
															   null,
															   android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
															   null,
															   null
															 );
						  while (phoneCursor.moveToNext()) {
							  String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
							  contactBean.setTel(phoneNumber);
						  }
						  phoneCursor.close();

						  return contactBean;
					  }
				  })
				  .subscribeOn(Schedulers.io())
				  .observeOn(AndroidSchedulers.mainThread())
				  .subscribe(new Observer<ContactBean>() {

					  @Override
					  public void onCompleted() {

						  Toast.makeText(
								  mContext,
								  "length:" + contacts.size(),
								  Toast.LENGTH_SHORT
										)
							   .show();
					  }

					  @Override
					  public void onError(Throwable e) {

						  Log.e(TAG,
								"onError: ",
								e

							   );

					  }

					  @Override
					  public void onNext(ContactBean contactBean) {

						  Log.i(
								  TAG,
								  "onNext: name:" + contactBean.getName() + " phone:" + contactBean.getTel()
							   );

						  contacts.add(contactBean);
					  }
				  });

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值