【问题标题】:Intent to open Instagram user profile on Android打算在 Android 上打开 Instagram 用户个人资料
【发布时间】:2014-02-25 16:11:47
【问题描述】:

我正在开发一个社交网络应用,我们的用户可以将他们的 Instagram 帐户连接到我们的服务。我想直接在他们的官方 Android 应用(如果已安装)中打开 Instagram 个人资料,但我找不到任何方法。然而,在他们的开发者网站上有一个page,关于 iOS 上完全相同的功能,但这似乎根本不适用于 Android。我在网上找到的所有内容都只是建议了在浏览器中打开链接的各种方法。有什么建议吗?

【问题讨论】:

  • @kentarosu 是的,但没有正确答案。
  • 阅读选择正确答案的 cmets。它说您此时无法将 IG 应用程序打开到个人资料,只能打开图片。最接近的解决方案是打开浏览器。

标签: android android-intent instagram


【解决方案1】:

我使用以下代码解决了这个问题。

Uri uri = Uri.parse("http://instagram.com/_u/xxx");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);

likeIng.setPackage("com.instagram.android");

try {
    startActivity(likeIng);
} catch (ActivityNotFoundException e) {
    startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://instagram.com/xxx")));
}

【讨论】:

  • 它打开浏览器
  • 看这个答案:stackoverflow.com/questions/15497261/… 你必须使用/_u/ 格式的配置文件
  • @IharobAlAsimi 这是非常基本的 Android 代码。并非所有事情都需要解释。
【解决方案2】:

尽管@jhondge 的解决方案有效且正确。这是一种更简洁的方法:

Uri uri = Uri.parse("http://instagram.com/_u/xxx");
    Intent insta = new Intent(Intent.ACTION_VIEW, uri);
    insta.setPackage("com.instagram.android");

    if (isIntentAvailable(mContext, insta)){
        startActivity(insta);
    } else{
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/xxx")));
    }

private boolean isIntentAvailable(Context ctx, Intent intent) {
    final PackageManager packageManager = ctx.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

【讨论】:

  • 我喜欢这个解决方案,但由于某种原因,当我尝试对 Facebook Intent 执行完全相同的操作时,它对我不起作用。不过,try/catch 方法适用于 Facebook,而您的方法适用于 Instagram Intent。您是否偶然知道是什么原因造成的?
  • @Krøllebølle 我想我遇到了同样的问题并转而尝试使用 Facebook 的 catch。不知道是什么原因造成的。
  • 这对我有用...我在没有在浏览器中打开 Instagram 应用程序的 Android 模拟器中尝试了它,我在安装了 Instagram 并打开 Instagram 应用程序的手机中尝试了它。谢谢。
【解决方案3】:

直接打开 Instagram 应用到用户个人资料:

String scheme = "http://instagram.com/_u/USER";
String path = "https://instagram.com/USER";
String nomPackageInfo ="com.instagram.android";
    try {
        activite.getPackageManager().getPackageInfo(nomPackageInfo, 0);
        intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(scheme));
        } catch (Exception e) {
            intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(path));
        }
        activite.startActivity(intentAiguilleur); 

// Use this link to open directly a picture
  String scheme = "http://instagram.com/_p/PICTURE";

【讨论】:

  • 它在浏览器中打开
【解决方案4】:

我试过这种方法,它对我有用..

instabtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent instaintent = getActivity().getPackageManager().getLaunchIntentForPackage("com.instagram.android");

                instaintent.setComponent(new ComponentName( "com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
                instaintent.setData( Uri.parse( "https://www.instagram.com/_u/bitter_truth_lol") );

                startActivity(instaintent);

            }
        });

【讨论】:

    【解决方案5】:

    根据@alex-karapanos 的回答,我使用以下代码:

    fun launchInsta() {
        val uriForApp: Uri = Uri.parse("http://instagram.com/_u/xxx")
        val forApp = Intent(Intent.ACTION_VIEW, uriForApp)
    
        val uriForBrowser: Uri = Uri.parse("http://instagram.com/xxx")
        val forBrowser = Intent(Intent.ACTION_VIEW, uriForBrowser)
    
        forApp.component =
            ComponentName(
                "com.instagram.android",
                "com.instagram.android.activity.UrlHandlerActivity"
            )
    
        try {
            startActivity(forApp)
        } catch (e: ActivityNotFoundException) {
            startActivity(forBrowser)
        }
    }
    

    【讨论】:

      【解决方案6】:

      @jhondge 答案的 Kotlin 版本:

                  val uriForApp: Uri = Uri.parse("http://instagram.com/_u/xxx")
                  val forApp = Intent(Intent.ACTION_VIEW, uriForApp)
      
                  val uriForBrowser: Uri = Uri.parse("http://instagram.com/xxx")
                  val forBrowser = Intent(Intent.ACTION_VIEW, uriForBrowser)
      
                  forApp("com.instagram.android")
      
                  try {
                      startActivity(context, forApp, null)
      
                  } catch (e: ActivityNotFoundException) {
                      startActivity(context, forBrowser, null)
      
                  }
      

      【讨论】:

        【解决方案7】:

        我在 webview 中使用片段实现了这一点,但我有一个问题, instagram 弹出 3 次:

        webView.setWebViewClient(new WebViewClient()
                {
         public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
                    {
         if(Uri.parse(urlx).getHost().endsWith("instagram.com")) {
        
                            gotoinstagram();
        
                          return false;
                        }
        
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                        viewx.getContext().startActivity(intent);
                        return true;
                    }
        
        
        
                });
        

        onCreateView 之外

        //instagram

        public void gotoinstagram()
        {
        
            Uri uri = Uri.parse("http://instagram.com/_u/XXXX");
            Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
        
            likeIng.setPackage("com.instagram.android");
        
            try {
                startActivity(likeIng);
            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://instagram.com/XXXX")));
            }
        
        }
        

        【讨论】:

          【解决方案8】:

          很多人已经回答了这个问题,但我仍在回答。使用上面回答的方法,工作流程要经过 3 个步骤。

          Step1:应用解析 uri 和 intent

          第二步:意图进入浏览器并加载 Instagram url

          Step3:浏览器然后重定向到 Instagram 应用

          但要直接打开 Instagram 应用程序而不用浏览器,您可以使用下面提到的方法。 创建如下 Intent 方法:-

          private Intent instaIntn(Context context) {
          Intent i1;
          String instaId = "your insta id";
          String appResolver = "instagram://user?username=";
          String webResolver = "https://instagram.com/";
          String instaPackageName = "com.instagram.android";
          String instaLitePackName = "com.instagram.lite";
          
          try {
          context.getPackageManager().getPackageInfo(instaPackageName, 0);
          i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(appResolver+instaId));
          } catch (PackageManager.NameNotFoundException e1) {
          Toast.makeText(getApplication(), "Instagram not found", Toast.LENGTH_SHORT).show();
          try {
          context.getPackageManager().getPackageInfo(instaLitePackName, 0);
          i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(appResolver+instaId));
          } catch (PackageManager.NameNotFoundException e2) {
          Toast.makeText(getApplication(), "Instagram and instagram lite not found", Toast.LENGTH_SHORT).show();
          i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(webResolver+instaId));
          }
          }
          return i1;
          }
          

          现在你可以通过简单地调用下面的语句从类内的任何地方开始这个意图:-

          startActivity(instaIntn(getApplicationContext()));
          

          现在它会做什么,它将尝试打开 Instagram 应用程序,如果未安装 instagram 应用程序,那么它将尝试打开 Instagram lite。如果这两个应用程序都丢失,那么它将意图浏览器。 上述方法意图用户访问 Instagram 应用程序和用户个人资料。您还可以让用户关注 Instagram 图片视频和其他页面。在此处查看完整文档https://developers.facebook.com/docs/instagram/sharing-to-feed/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多