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);
}
}
Related
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.
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>
I am writing a Spell Check client using the sample code in the SDK as an example. I have the following code (not actual implementation, but an accurate sample representation):
public class HelloSpellChecker implements SpellCheckerSessionListener {
private SpellCheckerSession mSpellCheckService;
private void bindService() {
final TextServicesManager tsm = (TextServicesManager) getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
mSpellCheckService = tsm.newSpellCheckerSession(null, null, this, true);
}
public void getSuggestions(String word) {
mSpellCheckService.getSuggestions(new TextInfo("tgis"), 3);
}
#Override
public void onGetSentenceSuggestions(final SentenceSuggestionsInfo[] arg0) {
Log.d(TAG, "onGetSentenceSuggestions");
// Process suggestions
}
}
What I want to know is will onGetSentenceSuggestions only be fired when my application calls getSuggestions, or will it be fired any time the system service receives a request to getSuggestions?
If it is the latter, what is the best way to ensure my app only processes suggestions which it requested?
I would say Android system service listener is localized through the session in this case.
onGetSentenceSuggestions method is fired by any request to getSuggestions method. However, you don't have to worry about processing suggestions which your app requested since the spellchecker session takes care of it. Your app only gets the suggestions requested by the session your app created to interact with the spell checker service.
Hope this helps.
References:
http://developer.android.com/reference/android/view/textservice/SpellCheckerSession.html
http://developer.android.com/guide/topics/text/spell-checker-framework.html
I'm considering possibility to replace Android's default SMS ContentProvider with my own one.
I'm talking about those which is called after:
context.getContentResolver().query(Uri.parse("content://sms/"),....);
I would dare to ask: is it possible?
No, That is internally used by the SMS messaging application AND the telephony layer of Android.
Replacing any in-built content providers is guaranteed to break Android - that is a given!
But what you can do is create your own content provider and craft your application to use your own instead.
If you're talking about monitoring the sms content provider, what you can do is use a ContentObserver to watch on the sms content provider and forward the changes made to the sms content provider to your own.
Here's an example of such scenario, everytime a change is made, the onChange gets fired, it is within there, that relaying to your own custom content provider will suffice.
private class MySMSContentObserver extends ContentObserver{
public MySMSContentObserver() {
super();
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// This is where the change notifications gets received!
}
}
// For example
MySMSContentObserver contentSMSObserver = new MySMSContentObserver();
//
context.getContentResolver().registerContentObserver (
"content://sms",
true,
contentSMSObserver);
Also, do not forget to unregister the content observer when the application is finished, i.e.:
context.getContentResolver().unregisterContentObserver(contentSMSObserver);
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.