4

I had developed an application for sending email. On click of email button it should ask user to choose one of email clients installed in phone. But, in my case additionally it is showing option of bluetooth which is not required. I searched alot but couldn't got any solution. Below I am posting my code.

public class EtestActivity extends Activity {
/** Called when the activity is first created. */
Button email;
Intent in;
private static final String TAG = "EmailLauncherActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    email = (Button)findViewById(R.id.email);
    email.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                in = new Intent(Intent.ACTION_SEND);
                in.setType("image/jpg");
                in.setType("plain/text");
                in.setType("application/octet-stream");
                in.putExtra(Intent.EXTRA_EMAIL, new String[]{"pranav_kotauniv@yahoo.co.in","friendlynitish@gmail.com"});
                in.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/android.jpg"));  
                startActivity(Intent.createChooser(in, "mailto:"));
            } catch (Exception e) {
                Log.e(TAG, "email sending failed", e);
            }//catch
        }//onClick
    });
}//onCreate

}//class

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • why are you calling setType() multiple times. – N-JOY May 02 '12 at 09:50
  • I removed in.setType("image/jpg"); and in.setType("plain/text"); But if I don't use setType("application/octet-stream") then it will show all applications in list such as bluetooth, gmail, facebook, twitter, messaging etc...and my requirement is only list of email clients. Do I need to set any other mime type? – Nitish May 02 '12 at 09:59

3 Answers3

2

try using ACTION_SENDTO instead of ACTION_SEND. this might resolve your problem.
Here is the API.
you can always use PackageManager and queryIntentActivities() and present your own custom dialog.

N-JOY
  • 10,344
  • 7
  • 51
  • 69
1

Code for sending email. Using PackageManager(http://developer.android.com/reference/android/content/pm/PackageManager.html), we can avoid getting different choices.

private void sendEpost(String type) {
boolean found = false;
Intent in = new Intent(android.content.Intent.ACTION_SEND);
in.setType("image/jpeg");
in.setType("application/octet-stream");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(in, 0);
if (!resInfo.isEmpty()){
    for (ResolveInfo info : resInfo) {
        if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                info.activityInfo.name.toLowerCase().contains(type) ) {
            in.putExtra(Intent.EXTRA_EMAIL, new String[]{});
            in.putExtra(Intent.EXTRA_SUBJECT,  "subject");
            in.putExtra(Intent.EXTRA_TEXT,     "your text");
            in.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath().toString()+"/diplomat/"+adapter.readName(touches))));
            in.setPackage(info.activityInfo.packageName);
            found = true;
            break;
        }
    }
    if (!found)
        return;
    startActivity(Intent.createChooser(in, "Select"));
}
}//sendEpost
Nitish
  • 3,097
  • 13
  • 45
  • 80
0

You have 2 option to solve this,

First is to,Change your Type of intent setType("message/rfc822"); Reffer this.

Second is to,Change your ACTION type Intent.ACTION_SENDTO Reffer this.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • As per your suggestion I tried both option. Using 1st: it still shows bluetooth in the chooser list. Using 2nd: Bluetooth options doesn't appears anymore, but, as I have to send attachment as well, it is not attaching file. Mine file is an jpeg image. – Nitish May 02 '12 at 11:24
  • Tried, didn't worked. Still facing same problem what I have mentioned in my previous comment. Can you please tell me how to use package manager for this purpose? – Nitish May 02 '12 at 11:37
  • Ya, I had already tried it with. In that case also bluetooth option appears. – Nitish May 02 '12 at 11:46