1
私はAndroidデバイスからアクセスしたときに電子メールとその情報をチェックしたいと思います。私はアンドロイドデバイスから電子メールにアクセスしたときに通知を受けたい。ContentObserverで電子メールにアクセスするには?
これはContentObserverを使用していますが動作しません。
私のコードは以下の通りです:
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;
}
}
}