2

I have made a app that can start a new email intent on a button click, I have tested it on several devices and it appears to work fine except that it crashes on galaxy 2 and galaxy 3. Here is my code checking for internet access then calling the intent.

ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if((cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected())) 
{
    Log.d("Main", "Start Email");

    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
    sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { monkey.getSendTo() });
    sendIntent.setData(Uri.parse(monkey.getSendTo()));
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "TFS note");
    sendIntent.setType("plain/text");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Sent From TFS\n");
    startActivity(sendIntent);

} else{//toast if there is no internet
    Toast.makeText(getActivity().getBaseContext(),"No Internet\n   access", 
    Toast.LENGTH_SHORT).show();
}

It looks like to me that on galaxy devices it is not fining the gmail app so it can't start up the intent, so I was wondering how I could go about making a generic intent to open any form of emailing app, or if there is a better way of making it compatible with galaxy devices.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
user2175493
  • 33
  • 1
  • 4

1 Answers1

4

Please delete:

sendIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");

That is insulting to the user, forcing them to use Gmail. It is the user's device, the user's bandwidth, the user's battery, the user's data, and the user's time. Please allow the user to send the user's data however the user wishes.

Besides, that activity may not exist in all versions of Gmail, and that activity may not be exported in all versions of Gmail.

Also, please change:

sendIntent.setType("plain/text");

to:

sendIntent.setType("text/plain");

as plain/text is not a valid MIME type.

You should also get rid of all the ConnectivityManager stuff. Not every means by which the user will want to share this information requires an immediate Internet connection.

None of this has anything to do with your crash, which is because you are referencing an activity (com.example.tfs.MainCalActivity) that does not exist in your APK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok thanks. I got it, and I did post the wrong log cat, sorry thats from another project I am working on – user2175493 Apr 07 '13 at 15:30
  • Beware that changing only the MIME type suggests an intent to view a text file. That likely won't display email clients. You can remedy this by specifying the action as `Intent.ACTION_SEND`, but this too is a poorly formulated intent. See also [this discussion between Mark and me](http://stackoverflow.com/q/1793752/154306). – Paul Lammertsma Apr 07 '13 at 15:37