我是Android开发的新手。我需要计算发送和接收的电子邮件数量。我已经尝试过链接中给出的代码如何获取未读gmail邮件的数量(在android上)。我正在运行Android 2.2中的代码。但即使我在清单文件中提到了权限,我也会收到Security异常。
java.lang.SecurityException:权限拒绝:从ProcessRecord打开提供程序com.google.android.gm.provider.MailProvider {40a44ec8 8832:com.android.gmailexample / 10090}(pid = 8832,uid = 10090 )需要com.google.android.gm.permission.READ_GMAIL或com.google.android.gm.permission.WRITE_GMAIL
以下是我的代码
package com.android.gmailexample;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class GmailCountActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
Log.d("Activity","Inside Oncrete");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
queryLabels();
}
public static final class LabelColumns {
public static final String CANONICAL_NAME = "canonicalName";
public static final String NAME = "name";
public static final String NUM_CONVERSATIONS = "numConversations";
public static final String NUM_UNREAD_CONVERSATIONS = "numUnreadConversations";
}
public void queryLabels(){
Log.d("Activity","Inside query label");
String account="email@company.com";
Uri LABELS_URI = Uri.parse("content://gmail-ls/unread/");
Uri ACCOUNT_URI = Uri.withAppendedPath(LABELS_URI, account);
ContentResolver contentResolver= this.getContentResolver();
Cursor cursor = contentResolver.query(ACCOUNT_URI, null, null, null, null);
//iterate over all labels in the account
if (cursor.moveToFirst()) {
int unreadColumn = cursor.getColumnIndex(LabelColumns.NUM_UNREAD_CONVERSATIONS);
int nameColumn = cursor.getColumnIndex(LabelColumns.NAME);
do {
String name = cursor.getString(nameColumn);
String unread = cursor.getString(unreadColumn);//here's the value you need
Log.d("Name", name);
Log.d("Unread ", unread);
} while (cursor.moveToNext());
Log.d("Unread count", String.valueOf(unreadColumn));
}
}
}
和清单文件是
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".GmailCountActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.providers.gmail.permission.READ_GMAIL"/>
<uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>
<uses-permission android:name="com.google.android.gm.permission.WRITE_GMAIL"/>
我无法找到错误。任何帮助将不胜感激。
此致 普什帕