获取重定向的URL从浏览器 - Android版 [英] Get Redirected URL From Browser - Android

查看:327
本文介绍了获取重定向的URL从浏览器 - Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题,我想从授权用户我的应用程序。
这个API提供了我的OAuth 2.0流程

我有一个包含我的应用程序键的链接A和我的重定向链接

如。的http://www.apiprovider?api-key=MY_API_KEY&redirect=<$c$c>REDIRECT_LINK

当在浏览器中打开一个链接,要求用户的用户名和密码,并授权我的应用程序,授权成功后,用户被重定向到 REDIRECT_LINK

REDIRECT_LINK 是带我需要在一个变量来存储关键codeD

如。 https://www.twitter.com/some_profile

我不希望用户访问重定向链接,我只是想为抓取网址,然后关闭浏览器 的页面不应该打开

我已经使用这个code在浏览器中打开链接

 意图httpIntent =新意图(Intent.ACTION_VIEW);
            httpIntent.setData(Uri.parse(LINK_A));
            startActivity(httpIntent);


解决方案

如果您在网页视图中打开网页,你的应用程序可以侦听与<的所有网址变更href=\"http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29\"相对=nofollow> shouldOverrideUrlLoading()回调。

如果您preFER打开浏览器的股票,你就可以开始,将定期轮询最新网址服务:

 字符串lastDate =0;
...
串newDate = Long.toString(Calendar.getInstance()getTimeInMillis());
光标光标= context.getContentResolver()查询(
    Browser.BOOKMARKS_URI,
    Browser.HISTORY_PROJECTION,
    Browser.BookmarkColumns.DATE +IS NOT NULL和+ Browser.BookmarkColumns.DATE +&GT;,
    lastDate,
    Browser.BookmarkColumns.DATE +DESC);
如果(cursor.moveToNext()){
    字符串URL = cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
}
cursor.close();lastDate = newDate; //更新时间戳

当你发现网​​址您的预期变化相匹配,您可以切换到你的活动,并继续您的应用程序的范围内。

有可能关闭浏览器,但它可能不会客气,从最终用户的POV。试想一下,比如,她有5个打开的选项卡的重要信息,而这一切都被摧毁!

请注意,这种方法的 可用于 的为Chrome浏览器,略有不同的内容提供商内容://com.android.chrome.browser/bookmarks

Here's my problem I want to authorize my app from a user. for this the api provides me OAuth 2.0 process

I have a link " A " which contains my app key and my redirection link

eg. http://www.apiprovider?api-key=MY_API_KEY&redirect=REDIRECT_LINK

link A when opened in a browser asks for the users username and password and authorizes my app, after successful authorization the user is redirected to REDIRECT_LINK

the REDIRECT_LINK is encoded with a key which I need to store in a variable

eg. https://www.twitter.com/some_profile?key=Asc545AS454dS44as

I dont want the user to visit the redirect link, i just want to fetch the url and then close the browser, the page should not open.

I have used this code to open the link in a browser

Intent httpIntent = new Intent(Intent.ACTION_VIEW);
            httpIntent.setData(Uri.parse("LINK_A"));
            startActivity(httpIntent); 

解决方案

If you open your page in WebView, you app can listen for any URL change with shouldOverrideUrlLoading() callback.

If you prefer to open the stock browser, you can start a service that will periodically poll the latest URL:

String lastDate = "0";
…
String newDate = Long.toString(Calendar.getInstance().getTimeInMillis());
Cursor cursor = context.getContentResolver().query(
    Browser.BOOKMARKS_URI,
    Browser.HISTORY_PROJECTION,
    Browser.BookmarkColumns.DATE + " IS NOT NULL AND " + Browser.BookmarkColumns.DATE + " > ?",
    lastDate,
    Browser.BookmarkColumns.DATE + " DESC");
if (cursor.moveToNext()) {
    String url = cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
}
cursor.close();

lastDate = newDate; // update the timestamp

When you find that the url matches your expected change, you can switch to your Activity and continue within the context of your app.

It is possible to close the browser, but it may not be polite, from the POV of your end-user. Imagine for example that she has 5 open tabs with important information, and all this is destroyed!

Note that this method can be used for Chrome browser, with slightly different content provider: content://com.android.chrome.browser/bookmarks

这篇关于获取重定向的URL从浏览器 - Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆