打算在 Android 上打开 Instagram 用户个人资料
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21505941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intent to open Instagram user profile on Android
提问by Grishka
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 pageon 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?
我正在开发一个社交网络应用程序,我们的用户可以将他们的 Instagram 帐户连接到我们的服务。我想直接在他们的官方 Android 应用程序中打开 Instagram 个人资料(如果已安装),但我找不到任何方法来做到这一点。但是,在他们的开发者网站上有一个关于 iOS 上完全相同功能的页面,但这似乎根本不适用于 Android。我在网上找到的所有内容都只是建议了在浏览器中打开链接的各种方法。有什么建议?
回答by jhondge
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")));
}
回答by Rahul Sainani
Although @jhondge's solution works and is correct. This is a more cleaner way to do this:
尽管@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;
}
回答by Kamal
To open directly instagram app to a user profile :
直接打开 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";
回答by Aditya Vyas-Lakhan
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);
}
});
回答by Alex Karapanos
Kotlin Version of @jhondge answer:
@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)
}
回答by silvertech048
I implemented this using fragment in webview but I have one issue, the instagram pop up comes three times :
我在 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;
}
});
outside of onCreateView
在 onCreateView 之外
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")));
}
}