Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从线性布局中按图像时打开默认应用程序_Java_Android_Onclick_Android Imageview - Fatal编程技术网

Java 从线性布局中按图像时打开默认应用程序

Java 从线性布局中按图像时打开默认应用程序,java,android,onclick,android-imageview,Java,Android,Onclick,Android Imageview,我有一个线性布局,其中包含三个图像: <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="0dp" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp" tools:c

我有一个
线性布局
,其中包含三个图像:

<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>
如果应用程序存在,我如何使用
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
     }
}

尝试使用此方法进行检查:

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

}

感谢我有类似的东西。在应用程序本身上,我可以直接将链接发送到我的页面吗?也不能将
用作变量谢谢。我有类似的东西。在应用程序本身上,我可以直接将链接发送到我的页面吗?
if (facebookIsInstalled()) {
    // Do something...

} else {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
    startActivity(browserIntent);

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