【问题标题】:if facebook app is not installed then it should open in browser如果未安装 facebook 应用程序,则应在浏览器中打开
【发布时间】:2015-08-06 22:52:49
【问题描述】:

我正在尝试在用户单击 imageview 时打开 facebook 页面。但如果安装了应用程序,那么它工作正常,但我想要的是如果未安装应用程序,那么它应该在浏览器中打开,以下是我的代码。 .

   iv_fb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            boolean installed = appInstalledOrNot("fb://page/pageid");  
            if(installed) {
                //This intent will help you to launch if the package is already installed
               /* Intent LaunchIntent = getPackageManager()
                    .getLaunchIntentForPackage("com.facebook.katana");*/

                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/pageid")));
                ContactActivity.this.finish();
               // startActivity(LaunchIntent);

                System.out.println("App is already installed on your phone");         
            } else {
                System.out.println("App is not currently installed on your phone");
            }

            // TODO Auto-generated method stub

        }
    });

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

【问题讨论】:

    标签: android facebook android-intent browser


    【解决方案1】:

    我使用这种方法:

    创建一个检查应用是否安装的方法:

    private boolean isPackageInstalled(String packagename, Context context) {
            PackageManager pm = context.getPackageManager();
            try {
                pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
                return true;
            } catch (NameNotFoundException e) {
                return false;
            }
        }
    

    如果未安装应用程序,请打开浏览器到您想要的页面。

    boolean FBInstalled = isPackageInstalled("com.facebook.katana",context);
                if(FBInstalled){
                    //do your stuff with the application
                }else{
                    Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(YOUR_SITE_URL));
                    startActivity(i);
                }
    

    【讨论】:

      【解决方案2】:

      尝试添加

       } else {
        Intent viewWebPage = new Intent(Intent.ACTION_VIEW, 
        Uri.parse("The url of the page you want to open if app isn't installed"));
        startActivity(viewWebPage);
      
       System.out.println("App is not currently installed on your device ");
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多