ContentObserver doesn't work for Browser Bookmarks on Nexus 7? - android

I register a ContentObserver to monitor Bookmarks changes of Chrome on Nexus 7, but there is not any callback get from onChange().
With the same code, I can get ContentObserver callback of Android default browser on other devices.
Does Chrome not support ContentObserver callback on Nexus 7?
Code as below:
getApplication().getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, observer);
...
static class HistoryOberser extends ContentObserver {
public HistoryOberser() {
super(null);
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(TAG, "onChange: " + selfChange);
}
}

Use URI "content://com.android.chrome.browser/bookmarks" for Android Crhome browser bookmarks can make the observer works.

Related

How to detect if the user turned on or off WiFi debug

I need to detect if the user has wifi debugging mode turned on or off when it opens the app or as soon as he changes it.
I already have a Broadcast receiver for the USB debugging, but can't find a away to detect the wi-fi one.
Thanks
private void detectWifiDebug() {
mSettingsObserver = new ContentObserver(mHandler) {
#Override
public void onChange(boolean selfChange, Uri uri) {
/* do stuff, for some reason when it turns on this is called twice */
}
};
getContentResolver().registerContentObserver(
Settings.Global.getUriFor("adb_wifi_enabled"), false,
mSettingsObserver);
}
I found this code that seems to work.

Android MMS observer

I have the following code:
public class MmsObserver extends ContentObserver {
private Context context;
public MmsObserver(Handler handler) {
super(handler);
this.context = service.getBaseContext();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Long largestDateCounted = Long.parseLong(UserPreferencesManager.getInstance().getValueFromPreferences(context, context.getString(R.string.preferences_current_counter), "0"));
String filter = "creator != ? and date > ?";
String[] args = new String[]{context.getPackageName(), Long.toString(largestDateCounted)};
Cursor c = context.getContentResolver().query(Constants.Mms, null, filter, args, null);
try {
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}
}
}
I'm trying to observe when user sends/receives an MMS message. However, my observer never gets called. Is there something I'm missing on this? I have read the below:
Android MMS Monitoring
Android MMS Broadcast receiver
EDIT
here is how i'm running the observer:
mmsContent = new MmsObserver(new Handler());
getContentResolver().registerContentObserver(Constants.Mms, true, mmsContent);
When registering a ContentObserver for MMS, the URI needs to be content://mms-sms/, at least on older Android versions. For some reason, content://mms/ won't work for a ContentObserver, other than possibly firing on changes to draft messages.
Do note that this will cause the Observer to fire for changes to the SMS table, as well.

Get Notification about new/changed/deleted contacts in Android when App is not running

it there a way to get noticed if a new or changed contact is made in Android? I want to get notified when the app starts, if there are any changes. Using a ContentObserver seems to me, that the app must run it in a activity. Or do i have to load all contacts every time from my DB and i am only able to recognize contact changed while my app runs and has an implemented ContentObserver?
i am only able to recognize contact changed while my app runs and has an implemented ContentObserver?
Correct, at least through Android 6.0.
The N Developer Preview has an enhanced JobScheduler that implements a ContentObserver for you, invoking your JobService when a change is detected. Unless there are problems, we can expect that enhanced JobScheduler to ship in the next release of Android, and you can opt into using it on newer Android devices.
Ok, what i did now is: Using a background service and build up an ContentObservice in the onCreate() function. Finally declaring it in the manifest. It will of course not work if the App is totally closed but if it is in background. Thats enough for me. It detects changes to the contacts. Are there any disadvantages in using this approach?
This is the service:
public class ContactsChangeService extends IntentService {
/**
* An IntentService must always have a constructor that calls the super constructor. The
* string supplied to the super constructor is used to give a name to the IntentService's
* background thread.
*/
public ContactsChangeService() {
super("ContactsChangeReceiver");
}
#Override
public void onCreate() {
super.onCreate();
//if created make an Observer
ContactsChangeObserver contentObserver = new ContactsChangeObserver();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentObserver);
Log.d(Constants.TAG, "[" + Constants.CONTACTS_OBSERVER_SERVICE + "] " + "started");
}
#Override
protected void onHandleIntent(Intent workIntent) {
// Gets data from the incoming Intent
String dataString = workIntent.getDataString();
//...
// Do work here, based on the contents of dataString
//...
}
}
This is the Observer:
public class ContactsChangeObserver extends ContentObserver{
public ContactsChangeObserver() {
super(null);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(Constants.TAG, "[" + Constants.CONTACTS_OBSERVER_SERVICE + "] " + "Change in Contacts detected");
}
}
And this is the manifest entry:
<service
android:name=".service.ContactsChangeService"
android:exported="true">
</service>

How to get email received event in Android

I could not find a way to listen email received event. Can any one please suggest me on this.
your valuable suggestions are highly appreciated.
The answer is no. 3rd party apps can't access the stock Email applications data.
I could succeed upto an extent, Now I could receive events for any changes happening in gmail account but still i am not clear about how to find only incoming mails.
Note:
I got some hacking techniques for getting this done but it will not work starting from froyo release.
currently I am using below code:
onChange method will get invoked for any changes happening in gmail.
mContext.getContentResolver().registerContentObserver(Uri.parse("content://gmail-ls/unread"), false, GmailObserver(new Handler() {}));
class GmailObserver extends ContentObserver {
public GmailObserver(Handler handler) {
super(handler);
}
#Override
public boolean deliverSelfNotifications() {
System.out.println("### ContentObserver deliverSelfNotifications");
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
System.out.println("### ContentObserver onChange");
super.onChange(selfChange);
}
}

Detecting new MMS (Android 2.1)

I'd like to recognize arrival of new MMS msg (after it is downloaded to inbox). I am doing the following:
private MMSContentObserver mMmsCO;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
h = new Handler();
mMmsCO = new MMSContentObserver(h);
getContentResolver().registerContentObserver (Uri.parse("content://mms"), true, mMmsCO);
}
where
private class MMSContentObserver extends ContentObserver {
public MMSContentObserver(Handler h) {
super(h);
}
#Override
public boolean deliverSelfNotifications() {
return false;
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
}
However, onChange is not getting called. What am I missing?
Thanks in advance.
The MMS content provider isn't part of the SDK but it can be used... a real answer here would be nice since all messaging apps use content://mms in some way or shape.
Since google decided not to standardize MMS we are all have to test on every phone out there but we still need to be able to handle MMSs in our apps.

Categories

Resources