Reputation: 4232
Hi i am trying to find Number of unread mails count from my Gmail account for this i searched lot in Google but i did not get any working solution and finally i found one document from below link i followed same process but it returns always Unread mails count as 0 but in Gmail account there is 2 Unread messages
http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html
Can some one help me please i am waiting for correct solution since 3 days
public static int getGmailCount(Context context) {
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(GmailContract.Labels.getLabelsUri("[email protected]"),
null,
null, null,
null);
if (cursor == null || cursor.isAfterLast()) {
Log.d(TAG, "No Gmail inbox information found for account.");
if (cursor != null) {
cursor.close();
}
return 0;
}
int count = 0;
while (cursor.moveToNext()) {
if (CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(cursor.getString(cursor.getColumnIndex(CANONICAL_NAME)))) {
count = cursor.getInt(cursor.getColumnIndex(NUM_UNREAD_CONVERSATIONS));
System.out.println("count is====>" + count);
break;
}
}
cursor.close();
return count;
}
Upvotes: 0
Views: 558
Reputation: 3994
I never tried this but can you try this.
public class MainActivity extends AppCompatActivity {
AccountManager accountManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accountManager = AccountManager.get(this);
Account account= getAccount(accountManager);
Log.d("MainActivity","UnreadCount-----> "+getUnreadCount(account.name));
}
public Account getAccount(AccountManager accountManager) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
public int getUnreadCount(String accountName) {
Cursor cursor = getContentResolver().query(
GmailContract.Labels.getLabelsUri(accountName),
UnreadQuery.PROJECTION, null, null, null
);
try {
if (cursor == null || cursor.isAfterLast()) {
return 0;
}
int unread = 0;
while (cursor.moveToNext()) {
String canonicalName = cursor.getString(UnreadQuery.CANONICAL_NAME);
int unreadInLabel = cursor.getInt(UnreadQuery.NUM_UNREAD_CONVERSATIONS);
if (GmailContract.Labels.LabelCanonicalNames.CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(canonicalName)) {
unread = Math.max(unread, unreadInLabel);
}
}
return unread;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private interface UnreadQuery {
String[] PROJECTION = {
GmailContract.Labels.NUM_UNREAD_CONVERSATIONS,
GmailContract.Labels.CANONICAL_NAME,
};
int NUM_UNREAD_CONVERSATIONS = 0;
int CANONICAL_NAME = 1;
}
}
Upvotes: 1