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

意图打开Whatsapp无法在Android OS 11中工作,但在Android(OS)10设备上工作正常,它会在android 11设备上显示消息“Whatsapp应用程序未安装在您的手机中”。有人对此有解决方案吗?

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();
        }

答案 1

android 11 对软件包可见性进行了新的更改。
您需要在应用标签下添加一个具有所需程序包名称的新部分:queries<manifest>

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

答案 2

"com.whatsapp"
也可能是罪魁祸首

我也对这个消息感到困惑。

问题是“whatsApp商业应用程序”,它有软件包名称:
"com.whatsapp.w4b"

使用以下代码找出安装了哪一个:

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;
}

推荐