How to access email with a ContentObserver?
<p>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.</p>

<p>I have used ContentObserver for this but it doesn't work.</p>

<p>My code is as below:</p>

<pre><code>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;
    }

}
</code></pre>

<p>}</p>
