A few days ago bhushan made an article about communicating between activites.
I'm currently working on a pet project that uses Oauth with the google data api, where
I had to get a response from the browser, so let's take a look at communicating between
an activity and a browser.
All the tutorials that I found on the net use onResume to get the response,
but I can't recommend that, because onResume runs even on the first run
of the app (see lifecycle) which is unnecessary in this case.
To sum up the logic:
- open the browser with a callback url provided
- user enters credentials
- the callback will take the result info back to the app that called the browser.
Opening the browser:
We need to use a service that uses browser callback, for example Google data api (Oauth) via Signpost.
We need to use a service that uses browser callback, for example Google data api (Oauth) via Signpost.
- ...
- String url = provider.retrieveRequestToken(consumer, "putyourappnamehere:///&q
uot;); - startActivity(new Intent(Intent.ACTION_VIEW, url));
- ...
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- setIntent(intent);//must store the new intent so getIntent() won't return the old one
- processResponse()
- }
- private void processResponse(){
- Intent intent = getIntent();
- Uri uri = this.getIntent().getData(); //get your data here
- }
You also need an intent filter in the manifest, that will "catch" the response.
- ...
- <intent-filter>
- <action android:name="android.intent.action.VI
EW"></action> - <category android:name="android.intent.category.
DEFAULT"></category> - <category android:name="android.intent.category.
BROWSABLE"></category> - <data android:scheme="putyourappnamehere"
></data> - </intent-filter>
- ...
0 comments :
Post a Comment