How to open the default mail inbox from android code?

随声附和 提交于 2019-12-06 17:23:40

问题


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(...)?

If so, what should be between the ( )?


回答1:


If the goal is to open the default email app to view the inbox, then key is to add an intent category and use the ACTION_MAIN intent like so:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL




回答2:


Yes, it's possible to open the Android default email inbox.
Use this code:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.email");
startActivity(intent);


This code works, you have to configure your Android device default mail first. If you already configured your mail it works fine. Otherwise, it force closes with a NullPointerException.




回答3:


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<ResolveInfo> 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<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        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);
    }



回答4:


  You can use this but it is for gmail only

  Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  
  emailIntent.setType("plain/text");
  startActivity(emailIntent); 



回答5:


You can simply use below code when for no attachment:

Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:support@mailname.com")); 
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback/Support");
startActivity(Intent.createChooser(emailIntent, "Send feedback"));

For details I recommend to visit: https://developer.android.com/guide/components/intents-common.html#Email




回答6:


Unfortunately it doesn't look promising. This has been asked before

How do I launch the email client directly to inbox view?

you can open the email client in compose mode, but you seem to already know that.




回答7:


Intent email = new Intent(Intent.ACTION_MAIN);

email.addCategory(Intent.CATEGORY_APP_EMAIL); startActivity(email);




回答8:


You can open Android default e-mail client using this:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.Welcome");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(emailIntent);


来源:https://stackoverflow.com/questions/8099017/how-to-open-the-default-mail-inbox-from-android-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!