这是我的服务界面
public interface ContactService {
void postPhoneContacts(@NonNull ContactServiceListener serviceListener, int id, List<UserProfileInfo> userInfo);
}
这是我想要进行网络通话的mybackground服务
如何在这里修改联系服务界面?我得到Null Pointer Exception。
从片段我开始这项服务。请帮我解决这个问题
public class UploadContactBgService extends Service {
private ContactService contactService;
private int userId;
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
List<UserProfileInfo> contactList = getContactsFromPhone();
EventBus.getDefault().post(new PostContactEvent(contactList));
/* SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
userId = prefs.getInt(LoginPresenter.PREF_USER_ID, 0);
if (userId != 0) {
postPhoneBookContactList(userId, contactList);
}*/
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
private List<UserProfileInfo> getContactsFromPhone() {
//Read Column names of ContactsContract table to get contact info from phone book
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
Uri DATA_CONTENT_URI = ContactsContract.Data.CONTENT_URI;
String ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String PHONE_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
String EMAIL_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
String DATA_CONTACT_ID = ContactsContract.Data.CONTACT_ID;
String ADDRESS_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
String ORG_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE;
String DATA_MIME_TYPE = ContactsContract.Data.MIMETYPE;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
String EVENT_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE;
String EVENT_START_DATE = ContactsContract.CommonDataKinds.Event.START_DATE;
String NOTE_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE;
String WEB_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE;
String RELATION_CONTENT_ITEM_TYPE = ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE;
int CONTACT_TYPE_MOBILE = 1;
int CONTACT_TYPE_WORK = 2;
int CONTACT_TYPE_HOME = 3;
int CONTACT_TYPE_WORK_FAX = 4;
int CONTACT_TYPE_HOME_FAX = 5;
int CONTACT_TYPE_PAGER = 6;
int CONTACT_TYPE_OTHER = 7;
int USER_PROFILE_TYPE = 2;
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(CONTENT_URI, null, null, null,
DISPLAY_NAME + " ASC ");
List<UserProfileInfo> userInfoList = new ArrayList<>();
if (cursor.moveToNext()) {
do {
String contactId = cursor.getString(cursor.getColumnIndex(ID));
UserProfileInfo userInfo = new UserProfileInfo();
UserProfile profile = new UserProfile();
UserType userType = new UserType();
/**
* Querying the table ContactsContract.Data to retrieve individual items like
home phone, mobile phone, work email etc corresponding to each contact
*/
Cursor dataCursor = cr.query(DATA_CONTENT_URI, null,
DATA_CONTACT_ID + "=" + contactId,
null, null);
if (dataCursor.moveToFirst()) {
/**
* checking if respective contactId has contact number or not
* If yes..then only add that user to list
* otherwise read next user
*/
int hasContactNumber = Integer.parseInt(dataCursor.getString(
dataCursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasContactNumber == 0) {
continue;
}
// Getting Display Name
String displayName = dataCursor.getString(dataCursor.getColumnIndex(DISPLAY_NAME));
profile.setName(displayName);
do {
// Getting Phone numbers
List<Contact> phones = new ArrayList<>();
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(PHONE_CONTENT_ITEM_TYPE)) {
Contact contactNum = new Contact();
ContactTypeDm contactTypeDm = new ContactTypeDm();
switch (dataCursor.getInt(dataCursor.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
String homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
userInfo.setUserName(homePhone);
contactNum.setContactNumber(homePhone);
contactTypeDm.setContactTypeId(CONTACT_TYPE_HOME);
contactNum.setContactTypeDm(contactTypeDm);
phones.add(contactNum);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
String mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
userInfo.setUserName(mobilePhone);
contactNum.setContactNumber(mobilePhone);
contactTypeDm.setContactTypeId(CONTACT_TYPE_MOBILE);
contactNum.setContactTypeDm(contactTypeDm);
phones.add(contactNum);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
String workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
userInfo.setUserName(workPhone);
contactNum.setContactNumber(workPhone);
contactTypeDm.setContactTypeId(CONTACT_TYPE_WORK);
contactNum.setContactTypeDm(contactTypeDm);
phones.add(contactNum);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
String other = dataCursor.getString(dataCursor.getColumnIndex("data1"));
userInfo.setUserName(other);
contactNum.setContactNumber(other);
contactTypeDm.setContactTypeId(CONTACT_TYPE_OTHER);
contactNum.setContactTypeDm(contactTypeDm);
phones.add(contactNum);
break;
}
userInfo.setContacts(phones);
}
// Getting EMails
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(EMAIL_CONTENT_ITEM_TYPE)) {
switch (dataCursor.getInt(dataCursor.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
String homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
profile.setEmail(homeEmail);
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
String workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
profile.setEmail(workEmail);
break;
}
}
// Getting Organization details
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(ORG_CONTENT_ITEM_TYPE)) {
String companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
String title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
profile.setOrgName(companyName);
profile.setTitle(title);
}
// Getting BDay
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(EVENT_CONTENT_ITEM_TYPE)) {
int indexEvent = dataCursor.getColumnIndex(EVENT_START_DATE);
String dobStr = dataCursor.getString(indexEvent);
profile.setBDay(dobStr);
}
//Getting Note
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(NOTE_CONTENT_ITEM_TYPE)) {
String note = dataCursor.getString(dataCursor.getColumnIndex("data1"));
profile.setNotes(note);
}
//Getting Postal Address Details...
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(ADDRESS_CONTENT_ITEM_TYPE)) {
String street = dataCursor.getString(dataCursor.getColumnIndex("data4"));
String city = dataCursor.getString(dataCursor.getColumnIndex("data7"));
String state = dataCursor.getString(dataCursor.getColumnIndex("data8"));
String postalCode = dataCursor.getString(dataCursor.getColumnIndex("data9"));
Address addressInfo = new Address();
addressInfo.setStreet(street);
addressInfo.setCity(city);
addressInfo.setZip(postalCode);
addressInfo.setState(state);
userInfo.setAddress(addressInfo);
}
//Getting Website
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(WEB_CONTENT_ITEM_TYPE)) {
String webUrl = dataCursor.getString(dataCursor.getColumnIndex("data1"));
profile.setWebsite(webUrl);
}
//Getting Relation
if (dataCursor.getString(dataCursor.getColumnIndex(DATA_MIME_TYPE)).equals(RELATION_CONTENT_ITEM_TYPE)) {
String relationship = dataCursor.getString(dataCursor.getColumnIndex("data1"));
profile.setRelationShip(relationship);
}
}
while (dataCursor.moveToNext());
}
userType.setUserTypeId(USER_PROFILE_TYPE);
profile.setUserType(userType);
userInfo.setUserProfile(profile);
userInfoList.add(userInfo);
} while (cursor.moveToNext());
}
return userInfoList;
}
public void postPhoneBookContactList(int id, List<UserProfileInfo> userInfoList) {
contactService.postPhoneContacts(new ContactServiceListener() {
@Override
public void onSuccess(UserProfileInfo responseBody) {
EventBus.getDefault().post(new GetSyncedContactListEvent(responseBody));
}
@Override
public void onError(String error) {
}
}, id, userInfoList);
}
}