I have created contact change observer, when a user any time change their contact need to know what change they did.
Step 1 :
public class ContactChangeObserver extends ContentObserver {
Context mContext;
public ContactChangeObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.e("Content Change", "Added New onChange selfChange");
}
#Override
public void onChange(boolean selfChange, Uri uri) {
Log.e("NoOf Content Change", "Contant Change Happen");
super.onChange(selfChange, uri);
if(mContext != null) {
Log.e("Content Change Name :", "start checking");
AppPreferences appPreferences = new AppPreferences(mContext);
String[] projection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
if (cursor.moveToFirst()) {
String sender = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String mobileno = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String status = "phone";
MaraContactsManager.getInstance().updateContactDB(mContext, mobileno, sender, status, appPreferences.getCountryCode());
}
}
}
Step 2: have register this by calling at HomeActivity
contactChangeObserver = new ContactChangeObserver(new Handler(),getApplicationContext());
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactChangeObserver);
Step 3: when i change any contact it goes to onChange Methode but I am not getting which contact changes done it says
java.lang.IllegalArgumentException: URI: content://com.android.contacts, calling user:
Please Help
Related
I access a db in an activity that is used in a different activity. However when i use getContentResolver.update() on the db it won't update in my CursorLoader although it accesses the same db (it's the same queryUri). It shows me the updated value when i dump a query to the db, however the CursorLoader won't.
Here is my onCreateLoader method:
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri queryUri;
if(id == ID_LOADER_PORTFOLIO) {
String[] projection = null;
String selection = null;
String[] selectionArguments = null;
queryUri = MainFeedContract.CONTENT_URI;
return new CursorLoader(this,
queryUri,
MainActivity.COLUMN_NAMES,
selection,
selectionArguments,
null);
}
return null;
}
and this is my call to update the db in the same activity:
portfolioBuilder
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton("Apply",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText et_newValue = (EditText) view.findViewById(R.id.et_portfolio);
Double value = Double.valueOf(et_newValue.getText().toString());
String selectedSpinnerCurrency = spinner.getSelectedItem().toString();
ContentValues cv = new ContentValues();
cv.put("units",value);
getContentResolver().update(MainFeedContract.CONTENT_URI, cv, "name=?", new String[]{selectedSpinnerCurrency});
Log.d("QUERY", DatabaseUtils.dumpCursorToString(getContentResolver().query(MainFeedContract.CONTENT_URI, null, null, null, null)));
}
});
Am i making a mistake when trying to update the db? :/
You may need to register a ContentObserver
Call this method in onResume()
public void registerDataObserver() {
try {
getContext().getContentResolver().registerContentObserver(MainFeedContract.CONTENT_URI, true, new DataObserver(new Handler()));
} catch (IllegalStateException ise) {
}
}
DataObserver class
class DataObserver extends ContentObserver {
public DataObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
getLoaderManager().restartLoader(0, null, this);
}
}
I am using a content observer to know that there is a change made to contact phonebook of the device but I am not getting the exact task done like whether the contact has been added, deleted or updated and what is the value of the modified contact.
// Service running in background which always run and check to know that content has been changed
public class ContactChange extends Service {
ContactObserver observer;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
observer = new ContactObserver(new Handler(),getApplicationContext());
// TODO Auto-generated method stub
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, false, observer);
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(observer);
}
}
//Content observer where we get to know that changes has made to the contact phonebook
public class ContactObserver extends ContentObserver {
private Context mContext;
DataBaseCurdOperation dataBaseCurdOperation;
ApiInterface apiInterface;
MyPrefs myPrefs;
ArrayList<InviteList> inviteArrayList;
public ContactObserver(Handler handler, Context context) {
super(handler);
this.mContext = context;
dataBaseCurdOperation = new DataBaseCurdOperation(mContext);
myPrefs = new MyPrefs(mContext);
apiInterface = ServiceGenerator.createService(ApiInterface.class, Config.BASE_URL_1);
inviteArrayList = new ArrayList<InviteList>();
}
#Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
Logger.LogError("URI", uri.toString());
boolean hasContactPermission = (ContextCompat.checkSelfPermission(mContext,
android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
if (hasContactPermission) {
SavingContactsActivity savingContactsActivity = new SavingContactsActivity(mContext);
savingContactsActivity.execute();
new InviteApiCall().execute();
}
}
Taking this approach and it is giving the contact whether it is added or updated not got the solution for deleted but surely will post the answer of deleted soon....
And I worked on the database after that
public class ContactSyncObserver extends ContentObserver {
Context mContext;
DataBaseCurdOperation dataBaseCurdOperation;
MyPrefs myPrefs;
public ContactSyncObserver(Handler handler, Context mContext) {
super(handler);
this.mContext = mContext;
dataBaseCurdOperation = new DataBaseCurdOperation(mContext);
myPrefs = new MyPrefs(mContext);
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
#Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
boolean hasContactPermission = (ContextCompat.checkSelfPermission(mContext,
Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
if (hasContactPermission) {
try {
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP + " Desc");
if (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Logger.LogError("contactId", myPrefs.getContactId());
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String rawContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
String phoneNumber = null;
String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhoneNumber) > 0) {
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
}
phones.close();
}
if (phoneNumber != null) {
phoneNumber = phoneNumber.replaceAll(" ", "");
}
if (dataBaseCurdOperation.checkIsContactIdExist(id)) {
if (!myPrefs.getContactId().equals(id)) {
dataBaseCurdOperation.updateNewNumber(id, phoneNumber, name, "updated");
UtilHandler.TriggerRefresh();
} else {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
myPrefs.setContactId("0");
}
}, 3000);
}
} else {
dataBaseCurdOperation.insertServerContact(id, name, phoneNumber, "inserted", "newNumber", "newName");
UtilHandler.TriggerRefresh(); // triggering my sync adapter here...
}
myPrefs.setContactId(id);
}
} catch (Exception e) {
Logger.LogError("Contact Exception", "occured");
}
}
}
}
I am developing an application in which i am working on Android Contacts and not able to move ahead. In app the need of application is that the contact which is updated should send to server or the contact which is deleted should send to server for sync.
I am using the contact service as:
public class ContactService extends Service {
private int mContactCount;
Cursor cursor = null;
static ContentResolver mContentResolver = null;
// Content provider authority
public static final String AUTHORITY = "com.android.contacts";
// Account typek
public static final String ACCOUNT_TYPE = "com.example.myapp.account";
// Account
public static final String ACCOUNT = "myApp";
// Instance fields
Account mAccount;
Bundle settingsBundle;
#Override
public void onCreate() {
super.onCreate();
// Get contact count at start of service
mContactCount = getContactCount();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Get contact count at start of service
this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver);
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
private int getContactCount() {
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null) {
return cursor.getCount();
} else {
cursor.close();
return 0;
}
} catch (Exception ignore) {
} finally {
cursor.close();
}
return 0;
}
private ContentObserver mObserver = new ContentObserver(new Handler()) {
#Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
new changeInContact().execute();
}
};
public class changeInContact extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0) {
ArrayList<Integer> arrayListContactID = new ArrayList<Integer>();
int currentCount = getContactCount();
if (currentCount > mContactCount) {
// Contact Added
} else if (currentCount < mContactCount) {
// Delete Contact
} else if (currentCount == mContactCount) {
// Update Contact
}
mContactCount = currentCount;
return "";
}
#Override
protected void onPostExecute(String result) {
contactService = false;
} // End of post
}
}
The issues i am facing are as follows :
A: In the above code for getting the recently updated contact i need to check the Version of each contact from device with my database stored version of contacts. Which took much time for large amount of contacts.
B. For getting deleted contact i need to check that the data for the Raw id stored in my database is present in device or not. If not then the contact is deleted. It also take too much time to check whole contacts.
But the same thing contact refresh is done in whats app in very few seconds like 2 to three seconds...
EDIT :
In the above code in following module :
if (currentCount > mContactCount) {
// Contact Added
Log.d("In","Add");
} else if (currentCount < mContactCount) {
// Delete Contact
Log.d("In","Delete");
} else if (currentCount == mContactCount) {
// Update Contact
Log.d("In","Update");
}
I put the log. So the update module is called many times, and also when i do add or delete that time too...
Please guide me and suggest me what to do to reduce the timing for the above tasks...
use the below query to get all the deleted and updated contacts.
public static final String ACCOUNT_TYPE = "com.android.account.youraccounttype"
public static final String WHERE_MODIFIED = "( "+RawContacts.DELETED + "=1 OR "+
RawContacts.DIRTY + "=1 ) AND "+RawContacts.ACCOUNT_TYPE+" = '"+ ACCOUNT_TYPE+"'";
c = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
null,
WHERE_MODIFIED,
null,
null);
I register SMS ContentObserver in my app. I want to update my ArrayList onChange with MessageID. This is my ObServer code..
public class SmsObserver extends ContentObserver {
private Context mContext;
private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms");
public SmsObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
public boolean deliverSelfNotifications() {
return true;
}
public void onChange(boolean selfChange) {
try{
Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
if (sms_sent_cursor != null) {
if (sms_sent_cursor.moveToFirst()) {
String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
if(protocol == null){
int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex ("type"));
if(type == 2){
smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));
}
}
}
} else {
Log.e("Info","Send Cursor is Empty");
}
} catch(Exception sggh) {
Log.e("Error", "Error on onChange : "+sggh.toString());
}
super.onChange(selfChange);
}
}
But, I am sending two messages. The first one is the phone closed. The second is the phone open. The first update is the latter. The second update is the first I sent the phone opens. With this ObServer I can access only the most recent post. Showing only the most recent data. How can I get the new updated data?
I want to check email and its information when accessed from my Android device. I want to get a notification when email is accessed from android device.
I have used ContentObserver for this but it doesn't work.
My code is as below:
public class EmailActivity extends Activity {
public MyContentObserver contentObserver = new MyContentObserver(new Handler());
ContentResolver cr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cr = this.getApplicationContext().getContentResolver();
this.getApplicationContext().getContentResolver()
.registerContentObserver (ContactsContract.CommonDataKinds.Email.CONTENT_URI,
true, contentObserver);
}
private class MyContentObserver extends ContentObserver {
public MyContentObserver(Handler h) {
super(h);
}
#Override
public void onChange(boolean selfChange) {
try
{
super.onChange(selfChange);
Uri callUri =ContactsContract.CommonDataKinds.Email.CONTENT_URI;
Cursor cur = cr.query(callUri, null, null, null, null);
while (cur.moveToNext()) {
String contact_id = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
String display_name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME));
String data = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String content_Type = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE));
String type = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.d("------ contact id : "+contact_id+"----", "----onChange fired by content ---observer--------");
Log.d("------display_name : "+display_name+"----", "----onChange fired by content ---observer--------");
Log.d("------data : "+data+"----", "----onChange fired by content ---observer--------");
Log.d("------content_Type : "+content_Type+"----", "----onChange fired by content ---observer--------");
Log.d("------type : "+type+"----", "----onChange fired by content ---observer--------");
}
}catch(Exception e){e.printStackTrace();
Log.d("------Excp----", "----exception come--------");
}
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
}
}
I don't think you can set up a ContentObserver on email accesses. It isn't part of the Android API currently.
In your code, when you reference the following content URI, you are actually looking at the text of a contact's email address (not their email content or access information):
ContactsContract.CommonDataKinds.Email.CONTENT_URI
Hope this helps!