计算在android中发送和接收的电子邮件数量

时间:2011-12-14 10:06:23

标签: android email

我是Android开发的新手。我需要计算发送和接收的电子邮件数量。 我已经尝试了链接How to get the number of unread gmail mails (on android)中给出的代码。我正在运行Android 2.2中的代码。但即使我在清单文件中提到权限,我也会收到安全异常。

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"/>

我无法找到错误。任何帮助将不胜感激。

此致 普什帕

2 个答案:

答案 0 :(得分:0)

不幸的是,邮件应用程序不公开其内容和数据,其中大多数是第三部分(包括gmail的封闭源代码) 因此,你想要的东西是不可接近的。

答案 1 :(得分:0)

我们需要ContentProvider来检索标签数据。我们可以使用它来访问特定帐户的收件箱和标签的最新未读计数。

http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html