简体   繁体   English

意图打开WhatsApp不起作用

[英]Intent to open WhatsApp does not work

I'm in my GridViewAdapter class, and I'm trying to open WhatsApp via Intent in my ImageView ClickListener, but it does not work: 我在GridViewAdapter类中,并且尝试通过ImageView ClickListener中的Intent打开WhatsApp,但它不起作用:

This is my Intent 这是我的意图

Context mContext;
Intent launchIntent =  mContext.getPackageManager().getLaunchIntentForPackage("com.whatsapp");
mContext.startActivity(launchIntent);

Why doesn't that work? 为什么不起作用? I got neither an error nor a success. 我既没有错误也没有成功。

EDIT: WHOLE CODE ADDED. 编辑:添加了整个代码。 All the other intents work, except for Whatsapp, what is my mistake? 除Whatsapp以外,其他所有目的均有效,我的错误是什么? I don't get it. 我不明白

public class CustomAndroidGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private final String[] string;
    private final int[] Imageid;

    public CustomAndroidGridViewAdapter(Context c, String[] string, int[] Imageid) {
        mContext = c;
        this.Imageid = Imageid;
        this.string = string;
    }

    @Override
    public int getCount() {
        return string.length;
    }

    @Override
    public Object getItem(int p) {
        return null;
    }

    @Override
    public long getItemId(int p) {
        return 0;
    }

    @Override
    public View getView(final int p, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.gridview_custom_layout, null);
            TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
            ImageView imageView = (ImageView) grid.findViewById(R.id.gridview_image);
            textView.setText(string[p]);
            imageView.setImageResource(Imageid[p]);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    switch (string[p]) {
                        case "Bluetooth":
                            Intent intentBluetooth = new Intent();
                            intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
                            mContext.startActivity(intentBluetooth);
                            break;
                        case "WhatsApp":
                            Intent launchIntent =  mContext.getPackageManager().getLaunchIntentForPackage("com.whatsapp");
                            mContext.startActivity(launchIntent);
                            break;

                        case "Messages":
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.addCategory(Intent.CATEGORY_APP_MESSAGING);
                            mContext.startActivity(intent);
                            break;

                        case "Spotify":
                            Intent i = null;
                            try {
                                i.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity"));
                                mContext.startActivity(i);
                            } catch (Exception e) {
                                Toast.makeText(mContext, "Spotify not available!", Toast.LENGTH_SHORT).show();
                            }

                            break;
                        case "Google Play Music":
                            Intent intent1 = mContext.getPackageManager().getLaunchIntentForPackage("com.google.android.music");
                            mContext.startActivity(intent1);
                            break;

                        case "E-Mails":
                            try {
                                Intent mailClient = new Intent(Intent.ACTION_VIEW);
                                mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
                                mContext.startActivity(mailClient);
                            } catch (Exception e) {
                                Toast.makeText(mContext, "No Mail Application is installed.", Toast.LENGTH_SHORT).show();
                            }
                            break;


                        case "Check Test":
                            break;

                        case "Navigation":
                            String uri = "http://maps.google.com/maps?saddr=";
                            Intent intent11 = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                            mContext.startActivity(intent11);
                            break;

                        case "Settings":
                            mContext.startActivity(new Intent(Settings.ACTION_SETTINGS));
                            break;


                    }

                }
            });

        } else {
            grid = (View) convertView;
        }


        return grid;
    }
}

EDIT 2 编辑2

   private final String[] string;
private final int[] Imageid;

public CustomAndroidGridViewAdapter(Context c, String[] string, int[] Imageid) {
    mContext = c;
    this.Imageid = Imageid;
    this.string = string;
}

In WhatsApp documentation , they say you should do the following: 他们在WhatsApp文档中说您应该执行以下操作:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

If you want to skip system app picker (which I believe is your case), add this line: 如果您想跳过系统应用选择器(我相信是您的情况),请添加以下行:

sendIntent.setPackage("com.whatsapp");

I recommend you to follow exactly this process. 我建议您完全遵循此过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM