How to receive email from contacts and phone numbers from contacts and class, does not have extension and oncreate () method?

I receive phone calls, letters from contacts with ext extend activity and oncreate .

Using the following code:

  \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Class A extends Activity{ new ClassB(this); } //////////////////////////////////////////////////// public static void getContactNumbers(Context context) { String contactNumber = null; int contactNumberType = Phone.TYPE_MOBILE; String nameOfContact = null; ContentResolver cr = context.getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur .getColumnIndex(BaseColumns._ID)); nameOfContact = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor phones = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (phones.moveToNext()) { contactNumber = phones.getString(phones .getColumnIndex(Phone.NUMBER)); contactNumberType = phones.getInt(phones .getColumnIndex(Phone.TYPE)); Log.i(TAG, "...Contact Name ...." + nameOfContact + "...contact Number..." + contactNumber); ApplicationConstants.phoneContacts .add(new ContactNumberBean(nameOfContact, contactNumber, contactNumberType)); } phones.close(); } } }// end of contact name cursor cur.close(); } /** * * This method is responsible to get native contacts and corresponding email * id (ApplicationConstants.emailContacts) * * @param context */ public static void getContactEmails(Context context) { String emailIdOfContact = null; int emailType = Email.TYPE_WORK; String contactName = null; ContentResolver cr = context.getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur .getColumnIndex(BaseColumns._ID)); contactName = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // Log.i(TAG,"....contact name....." + // contactName); cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + id, null, null); while (emails.moveToNext()) { emailIdOfContact = emails.getString(emails .getColumnIndex(Email.DATA)); // Log.i(TAG,"...COntact Name ...." // + contactName + "...contact Number..." // + emailIdOfContact); emailType = emails.getInt(emails .getColumnIndex(Phone.TYPE)); ApplicationConstants.emailContacts .add(new ContactEmailBean(contactName, emailIdOfContact, emailType)); } emails.close(); } }// end of contact name cursor cur.close(); } ///////////////////////////////////////// 

It works fine with getting results, but I don't know how to implement the code in the above example:

 ApplicationConstants.phoneContacts .add(new ContactNumberBean(nameOfContact, contactNumber, contactNumberType)); 

If anyone knows this, please help me.

+6
source share
1 answer

You only need access to the android.content.Context object to access the ContentResolver and thus the ContentProviders request. The action expands the context, so it works. android.app.Service also extends Activity, so it works too. android.app.Application also expands the context, so this will work too.

+3
source

Source: https://habr.com/ru/post/890571/


All Articles