1

我有一个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>

我在我的活动中称它们为(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 的 Web 浏览器?

//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
     }
}
4

2 回答 2

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

}
于 2013-08-15T18:03:29.670 回答
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 ;
        }
于 2013-08-15T18:09:42.323 回答