开发者

How to make ImageButton directly open email composer on click?

I have made one application for epaper/emagazine in which I want to give an imagebutton for email composer that if I click on that button it will directly open the email composer inserting all the data of that page to the email composer message body asking only recipient address.

I have the output but instead of email it opens popup list asking messaging and bluetooth.

This is my code:

final Intent emailIntent = new Intent(Intent.开发者_如何转开发ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "lets.think.android@gmail.com" });
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "App Error Report");
emailIntent.putExtra(Intent.EXTRA_TEXT, "stacktrace");
activity(Intent.createChooser(emailIntent, "Send error report..."));


Call sendEmail() method on button click:

final Context context = getApplicationContext();    
Button button = (Button) findViewById(R.id.openpdfbutton);          
button.setOnClickListener(new OnClickListener() {               
    public void onClick(View arg0) {
        sendEmail(context, new String[]{"abc@xyz.com"}, "Sending Email",
                  "Test Email", "I am body");
    }
});

Define the sendEmail() method:

public static void sendEmail(Context context, String[] recipientList,
            String title, String subject, String body) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);    
    emailIntent.setType("plain/text");    
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
    context.startActivity(Intent.createChooser(emailIntent, title));
}

And set permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />


Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

using intent we don't need permission..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜