0

I'm building an app to handle anything that concerns the academic life of an university student, that is including his university mail account.

Let's say the phone where my app is installed has two or more different gmail account set:
- "account 1" - "abcdef@gmail.com"
- "account 2" - "123456@mydomain.com"

I know that to start the gmail app I could use this code:

public void showMailBox(View view){  
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);  
    sendIntent.setType("plain/text");  
    sendIntent.setData(Uri.parse("test@gmail.com"));  
    sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");  
    sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });  
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test");  
    sendIntent.putExtra(Intent.EXTRA_TEXT, "hello");  
    startActivity(sendIntent);  
}

How could I set an intent to start Gmail, or Inbox, with the second account set as sender?

ParKein
  • 133
  • 1
  • 12
  • "I know that to start the gmail app I could use this code" -- only for certain versions of the Gmail app. As soon as they refactor their code, your code will break. Also, `plain/text` is not a MIME type. Beyond that, Gmail does not have a documented or supported on-device API for manipulating their app. – CommonsWare Mar 01 '15 at 18:05

1 Answers1

0

For accessing the registered accounts in your Android phone, you must add android.permission.GET_ACCOUNTS permission to your Manifest file. This permission allows access to the list of accounts in the Accounts Service.

<uses-permission android:name=”android.permission.GET_ACCOUNTS” />

Use the below method to get the list of accounts(google account ids) in your mobile.

public ArrayList<String> getData() {
        ArrayList<String> accountsall = new ArrayList<String>();
            try {
            Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
            for (Account account : accounts) {
                accountsall.add(account.name);
            }
        } catch (Exception e) {
            Log.i("Exception", "Exception:" + e);
        }
        return accountsall;
    }

Get the accounts from the list returned by the method in your code,verify which account you need where.Here for example the first found account I have set it as To mail id and the second found as the From mail id. Just try printing the list to find which account is in which position and later reference in your code.

public void showMailBox(View view){  
    ArrayList<String> mails=getData();
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);  
    sendIntent.setType("plain/text");  
    sendIntent.setData(Uri.parse(mails.get(0)));  
    sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");  
    sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { mails.get(1) });  
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test");  
    sendIntent.putExtra(Intent.EXTRA_TEXT, "hello");  
    startActivity(sendIntent);  
}
Psypher
  • 10,717
  • 12
  • 59
  • 83