问题
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);
回答1:
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
来源:https://stackoverflow.com/questions/16353973/get-redirected-url-from-browser-android