我有LinearLayout
,其中包含三张图片:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity"
android:orientation="horizontal"
android:layout_gravity="center" >
<ImageView
android:id="@+id/imgFB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fb"
android:padding="5dp" />
<ImageView
android:id="@+id/imgTW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tw"
android:padding="5dp" />
<ImageView
android:id="@+id/imgLIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lin"
android:padding="5dp" />
</LinearLayout>
我在我的Activity中调用它们(因为dialog
之前被调用):
ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
ImageView iv2 = (ImageView) dialog.findViewById(R.id.imgTW);
ImageView iv3 = (ImageView) dialog.findViewById(R.id.imgLIN);
我如何使用onClick()
方法打开应用程序(如果存在)或打开附加了URL的网络浏览器?
//Example
public void onClick(View v) {
if (iv1 is clicked()) {
Check if FB is installed, if it is take them to my FB page
If FB is not installed, open the browser and take them to my FB page
}
}
答案 0 :(得分:1)
尝试使用此方法检查:
public boolean facebookIsInstalled() {
try{
ApplicationInfo info = getPackageManager().
getApplicationInfo("com.facebook.katana", 0 );
return true;
} catch( PackageManager.NameNotFoundException e ){
return false;
}
}
然后在onClick方法上:
if (facebookIsInstalled()) {
// Do something...
} else {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
}
答案 1 :(得分:1)
boolean installed = appInstalledOrNot("com.example.package");
if(installed){
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
final ComponentName cn = new ComponentName("com.example.package",
"com.example.package.ActivityClass" );
intent.setComponent(cn);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}else{
// not installed
String marketUri = "market://details?id=com.example.package";
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse(marketUri));
startActivity(goToMarket);
}
}
public static boolean appInstalledOrNot(String package)
{
PackageManager pm = getPackageManager();
boolean app_installed;
try
{
pm.getPackageInfo(package, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}