How to open the default mail inbox from android code?

前端 未结 9 1645
执念已碎
执念已碎 2021-02-19 00:43

I\'m trying to link a button to the mail app. Not to send mail, but just to open the inbox.

Should I do this with Intent intent = new Intent(...)?

9条回答
  •  误落风尘
    2021-02-19 00:54

    This code worked for me. It opens a picker with all email apps registered to device and straight to Inbox:

    Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
        PackageManager pm = getPackageManager();
    
        List resInfo = pm.queryIntentActivities(emailIntent, 0);
        if (resInfo.size() > 0) {
            ResolveInfo ri = resInfo.get(0);
            // First create an intent with only the package name of the first registered email app
            // and build a picked based on it
            Intent intentChooser = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
            Intent openInChooser =
                    Intent.createChooser(intentChooser,
                            getString(R.string.user_reg_email_client_chooser_title));
    
            // Then create a list of LabeledIntent for the rest of the registered email apps 
            List intentList = new ArrayList();
            for (int i = 1; i < resInfo.size(); i++) {
                // Extract the label and repackage it in a LabeledIntent
                ri = resInfo.get(i);
                String packageName = ri.activityInfo.packageName;
                Intent intent = pm.getLaunchIntentForPackage(packageName);
                intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
            }
    
            LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
            // Add the rest of the email apps to the picker selection
            openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
            startActivity(openInChooser);
        }
    

提交回复
热议问题