Intent to open Instagram user profile on Android

后端 未结 6 1572
清歌不尽
清歌不尽 2020-12-02 10:38

I\'m developing a social networking app and our users can connect their Instagram account to our service. I\'d like to open Instagram profiles directly in their official And

相关标签:
6条回答
  • 2020-12-02 10:57

    I solved this problem using the following code.

    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")));
    }
    
    0 讨论(0)
  • 2020-12-02 10:57

    To open directly instagram app to a user profile :

    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";
    
    0 讨论(0)
  • 2020-12-02 10:58

    Kotlin Version of @jhondge answer:

                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)
    
                }
    
    0 讨论(0)
  • 2020-12-02 11:00

    I tried this way and it worked for me..

    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);
    
                }
            });
    
    0 讨论(0)
  • 2020-12-02 11:06

    I implemented this using fragment in webview but I have one issue, the instagram pop up comes three times :

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

    outside of 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")));
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 11:09

    Although @jhondge's solution works and is correct. This is a more cleaner way to do this:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题