Communicating between an activity and the browser - callback
A few days ago Gabor 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.
- ...
- 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>
- ...
Please note that in the code it should look like this: "putyourappnamehere:///", and in the manifest it's just "putyourappnamehere".
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 45 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 45 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 45 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 45 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 45 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN