0

I am new to android development.I have a requirement to count the number of email sent and received. I have tried the code given in the link How to get the number of unread gmail mails (on android). I am running the code in android 2.2. But I am getting Security exception even though i have mention permission in manifest file.

java.lang.SecurityException: Permission Denial: opening provider com.google.android.gm.provider.MailProvider from ProcessRecord{40a44ec8 8832:com.android.gmailexample/10090} (pid=8832, uid=10090) requires com.google.android.gm.permission.READ_GMAIL or com.google.android.gm.permission.WRITE_GMAIL

Here is my code

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));
        }
    }
   }

and my manifest file is

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

I am not able to locate the error. Any help will be appreciated.

Regards Pushpa

Community
  • 1
  • 1
pushpa
  • 573
  • 2
  • 7
  • 19
  • This a good answer to your question i guess, [How to get the number of unread gmail mails (on android)](http://stackoverflow.com/questions/2992635/how-to-get-the-number-of-unread-gmail-mails-on-android) – Yaqub Ahmad Dec 14 '11 at 10:23
  • I'm not sure if this qualifies as an exact duplicate, but the question linked by @YaqubAhmad is _strongly_ related. – Tim Post Dec 14 '11 at 11:31
  • @ Yaqub Ahmad: Thanks. This link helps me to try different approach. – pushpa Dec 15 '11 at 06:48

2 Answers2

0

We need ContentProvider to retrieve label data. We can use this to access up-to-date unread counts for specific accounts’ inboxes and labels.

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

0

Unfortunately mail applications do not expose their content and data, and most of them are third part (closed source including gmail) Thus, the thing you want is not approachable.

Abhinava
  • 1,030
  • 9
  • 19