Intent to open Instagram user profile on Android

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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 Android app (if it is installed) but I can't find any way to do that. However, there is a page on their developer site about the exact same feature on iOS but this doesn't seem to work on Android at all. Everything I found on the web only suggests various ways to open links in a browser. Any suggestions?

回答1:

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


回答2:

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 list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);     return list.size() > 0; } 


回答3:

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


回答4:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!