简体   繁体   English

通过意图打开whatsapp在Android 11中不起作用

[英]Opening whatsapp through intent not working in Android 11

Opening Whatsapp with intent is not working in android OS 11 but working fine up to android (OS) 10 devices, It displays the message "Whatsapp app not installed in your phone" on the android 11 device.有意打开 Whatsapp 无法在 android OS 11 中运行,但在 android (OS) 10 设备上运行良好,它会在 android 11 设备上显示消息“Whatsapp app not installed in your phone”。 Does anyone have a solution for this?有人对此有解决方案吗?

String contact = "+91 9999999999"; // use country code with your phone number
        String url = "https://api.whatsapp.com/send?phone=" + contact;
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            context.startActivity(i);
        } catch (PackageManager.NameNotFoundException e) {
          Toast.makeText(mContext, "Whatsapp app not installed in your phone",Toast.LENGTH_LONG).show();
           e.printStackTrace();
        }

There are new changes in android 11 of package visibility . android 11 的package visibility有了新的变化。
You need to add a new section queries under you app's <manifest> tag with desired package name:您需要在应用的<manifest>标记下添加一个新的部分queries ,其中包含所需的包名称:

<manifest package="com.example.app">
    <queries>
        <package android:name="com.whatsapp" />
    </queries>
  ...
</manifest>

"com.whatsapp"
can also be the culprit .也可以是罪魁祸首

i was also boggled with this message.我也对这条消息感到困惑。

the issue was "whatsApp business app" which has package name:问题是具有包名称的“whatsApp 业务应用程序”
"com.whatsapp.w4b"

used following code to find out which one is installed:使用以下代码找出安装了哪一个:

String appPackage="";
if (isAppInstalled(ctx, "com.whatsapp.w4b")) {
    appPackage = "com.whatsapp.w4b";
    //do ...
} else if (isAppInstalled(ctx, "com.whatsapp")) {
    appPackage = "com.whatsapp";
    //do ...
} else {
    Toast.makeText(ctx, "whatsApp is not installed", Toast.LENGTH_LONG).show();
}

private boolean isAppInstalled(Context ctx, String packageName) {
    PackageManager pm = ctx.getPackageManager();
    boolean app_installed;
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}

Instead of using wildcards, it's more explicit to add both package names:与其使用通配符,不如添加两个包名更明确:

<manifest package="com.example.app">
    <queries>
        <package android:name="com.whatsapp"/>
        <package android:name="com.whatsapp.w4b"/>
    </queries>
  ...
</manifest>

Rather than adding each Package names in , you can add:您可以添加以下内容,而不是添加每个包名称:

**<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />**

to your AndroidManifest.xml file in your project.到项目中的 AndroidManifest.xml 文件。 I was also bothering with the same, this permission allowed me to troubleshoot my issue/error.我也有同样的烦恼,这个权限让我可以解决我的问题/错误。

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

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