1. 弱引用(WeakReference或SoftReference)
class DispatcherCallback implements IDispatcherCallback {
private WeakReference<Activity> wraRef;
public IDispatcherCallback setActivity(Activity activity) {
wraRef = new WeakReference<Activity>(activity);
return this;
}
}
//在其他地方因回调使用的时候
public void onOperationFinished(final int nSign, final WeakReference<Activity> wraRef) {
if (wraRef == null) {
return;
}
final Activity activity = wraRef.get();
if (activity == null) {
return;
}
//下面根据传入的activity自行操作处理
}
2. 获得手机联系人
public class PhoneInfo {
// 通讯录手机号码
public String strPhoneNum;
// 通讯录手机号对应的人员名称
public String strPhoneName;
}
//获取手机联系人
public List<PhoneInfo> getPhoneContacts(Context context) {
if (context == null)
return null;
ArrayList<PhoneInfo> listPhone = new ArrayList<PhoneInfo>();
// 得到ContentResolver对象
ContentResolver cr = context.getContentResolver();
// 取得电话本中开始一项的光标
Cursor cursorURI = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// 向下移动光标
while (cursorURI.moveToNext())
{
// 取得联系人名字
int nNameFieldColumnIndex = cursorURI.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String strDisplayName = cursorURI.getString(nNameFieldColumnIndex);
// 取得电话号码
String strContactId &