我想使用 ContentProvider 从默认的 HTC 邮件应用程序读取来自 Android 设备 (Wildfire S) 的电子邮件,类似于读取 SMS:
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
smss.add(new SMSObject(c)); //custom object which just retrieves all fields
}
邮件也应该有类似的东西,所以我检查了所有可用的 ContentProviders:
for (PackageInfo pack : getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
Log.d("Example", "provider: " + provider.authority);
}
}
}
在此列表中(其中包含 , 和 之类的东西gmail-ls
,sms
所以com.android.calendar
我在正确的轨道上)我找到了mail
,所以我尝试了这个:
Uri allMessage = Uri.parse("content://mail/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
emails.add(new SMSObject(c)); //custom object. should work with emails as well
}
但我得到了这个例外:
11-14 11:48:42.105: E/AndroidRuntime(3609): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.me.emailretriever/nl.me.emailretriever.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.htc.android.mail.MailProvider from ProcessRecord{40685dd8 3609:nl.me.emailretriever/10076} (pid=3609, uid=10076) requires com.htc.android.mail.permission.READ_MAIL or com.htc.android.mail.permission.WRITE_MAIL
...
11-14 11:48:42.105: E/AndroidRuntime(3609): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.htc.android.mail.MailProvider from ProcessRecord{40685dd8 3609:nl.me.emailretriever/10076} (pid=3609, uid=10076) requires com.htc.android.mail.permission.READ_MAIL or com.htc.android.mail.permission.WRITE_MAIL
所以我添加了这两个权限,但无济于事。
<uses-permission android:name="com.htc.android.mail.permission.WRITE_MAIL" />
<uses-permission android:name="com.htc.android.mail.permission.READ_MAIL" />
我怎样才能像访问 SMS 一样阅读/访问电子邮件?为什么我得到一个例外,虽然我有所需的权限?