我试图在用户点击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;
}
答案 0 :(得分:2)
我使用这种方法:
如果安装了应用,请创建一个chek的方法:
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);
}
答案 1 :(得分:1)
尝试添加
} 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 ");
}
}