Android email chooser

前端 未结 3 1835
遇见更好的自我
遇见更好的自我 2020-12-10 00:04

I am writing an app that needs to send emails at the end of each transaction. I am doing the following:

Intent mail = new Intent(Intent.ACTION_SEND);
mail.s         


        
相关标签:
3条回答
  • 2020-12-10 00:32

    You would have to create your own chooser, possibly as an AlertDialog populated using the results of calling queryIntentActivities() on PackageManager.

    0 讨论(0)
  • 2020-12-10 00:32

    Here is the solution:

    private void setSpinnerValues() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/html");
        PackageManager pm = getPackageManager();
        emailers = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER);
    
        if (emailers.size() == 0) {
            spnEmailProgram.setEnabled(false);
            return;
        }
        spnEmailProgram.setEnabled(true);
        CharSequence[] sa = new CharSequence[emailers.size()];
        int lastPosition = 0;
        for (int i = 0; i < emailers.size(); i++) {
            ResolveInfo r = emailers.get(i);
            sa[i] = pm.getApplicationLabel(r.activityInfo.applicationInfo);
            if (r.activityInfo.name.equalsIgnoreCase(Options.EmailClass)) {
                lastPosition = i;
            }
        }
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_item, sa);
        adapter.
                  setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnEmailProgram.setAdapter(adapter);
        spnEmailProgram.setSelection(lastPosition);
    }
    

    Save the choice for later use:

        if (emailers.size() == 0) {
            Options.EmailProgram = "";
            Options.EmailClass = "";
        } else {
            ResolveInfo r = emailers.get(spnEmailProgram.getSelectedItemPosition());
            Options.EmailProgram = r.activityInfo.packageName;
            Options.EmailClass = r.activityInfo.name;
        }
    

    Now, to consume it, just to the following:

    Intent mail = new Intent(Intent.ACTION_SEND);
    mail.setType("text/html");
    Intent chooser = null;
    if (Options.EmailProgram!=null && Options.EmailProgram.length()>0) {
      mail.setClassName(Options.EmailProgram,Options.EmailClass);
      chooser = mail;
    }
    

    fill in rest of data and start the activity

    if (chooser == null) {
      chooser = Intent.createChooser(mail,"Select Email Software..."); 
    }
    startActivity(chooser);
    
    0 讨论(0)
  • 2020-12-10 00:41

    It's a common misconception to use text/plain or text/html. This will trigger any application that can handle plain or HTML text files without any context, including Google Drive, Dropbox, Evernote and Skype.

    Instead use a ACTION_SENDTO, providing the mailto: Uri:

    intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    

    You can then proceed using the chooser as suggested through the other answers.

    0 讨论(0)
提交回复
热议问题