Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

How to open Gmail Compose when a button is clicked in Android App?

I am trying to open up Gmail Compose screen when a button is clicked in my Android App. Do I need some API key for this from Google? or what do I need to do in my button onClickListener?

Any kind of insight is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)

The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.

package com.stackoverflow.beekeeper;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import java.util.List;

public class StackOverflowTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        final PackageManager pm = getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
        ResolveInfo best = null;
        for (final ResolveInfo info : matches)
           if (info.activityInfo.packageName.endsWith(".gm") ||
        info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
        if (best != null)
           intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
        startActivity(intent);
    }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...