android - twitter: callback is received but not shown
I'm trying to figure out for 4 days why my app is not functioning as I expect it to do.
My app wants to use OAuth with twitter (Is working) and on the call back it should enter the onNewIntent method. It's not doing that and I can't figure out why.
So clicking the twitterbutton opens a webpage where I can enter my credentials. Then I get a screen stating that it was successful and that I will be redirected back to my app. I do get back to my app but the onNewIntent method is never called. I have put a breakpoint at the start of the onNewIntend method and started the debugger. It does not stop on that method! It does stop in other methods so it can't be the debugger.
In my log file (included), third line from the bottom, you can see that it is starting a new intent. So..... what is going on here?
Can someone please explain? I am very confused.
This is my code that gets triggered when the twitterbutton is clicked:
callBackURL = "myapp://twitactivity";
httpOauthConsumer = new CommonsHttpOAuthConsumer(consKey, consSec);
httpOauthProvider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token","http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize");
String authUrl = httpOauthProvider.retrieveRequestToken(httpOauthConsumer, callBackURL);
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(authUrl)));
This is the starting code of my onNewIntent method:
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i("onNewIntent", "Yep success.");
Uri uri = intent.getData();
if(uri != null && uri.toString().startsWith(callBackURL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
etc......
I also have the following in my manifest file:
<activity android:name=".TwitterScreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="twitactivity" />
</intent-filter>
</activity>
And this is what I see in my log file:
01-24 10:23:36.064: INFO/ActivityManager(61): Displayed activity nl.gemoro.android.demo/.TwitterScreen: 6660 ms (total 6660开发者_运维问答 ms)
01-24 10:23:38.614: INFO/global(348): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
01-24 10:23:44.025: INFO/ActivityManager(61): Starting activity: Intent { act=android.intent.action.VIEW dat=http://twitter.com/oauth/authorize?oauth_token=KjCUNMKg13OClyRjQff94QWKfoRBUpNLE2uF9cJkHA cmp=com.android.browser/.BrowserActivity }
01-24 10:23:44.185: INFO/ActivityManager(61): Start proc com.android.browser for activity com.android.browser/.BrowserActivity: pid=355 uid=10034 gids={3003, 1015}
01-24 10:23:44.724: INFO/ActivityThread(355): Publishing provider browser: com.android.browser.BrowserProvider
01-24 10:23:46.704: INFO/ActivityManager(61): Displayed activity com.android.browser/.BrowserActivity: 2544 ms (total 2544 ms)
01-24 10:23:53.624: WARN/KeyCharacterMap(355): No keyboard for id 0
01-24 10:23:53.624: WARN/KeyCharacterMap(355): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-24 10:23:54.574: DEBUG/dalvikvm(348): GC_EXPLICIT freed 3965 objects / 267064 bytes in 2364ms
01-24 10:24:01.064: INFO/ActivityManager(61): Starting activity: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=myapp://twitactivity?oauth_token=KjCUNMKg13OClyRjQff94QWKfoRBUpNLE2uF9cJkHA&oauth_verifier=41owXswx5TsxHhRyiviFRkRvcpdekm7akRa2IFFM cmp=nl.gemoro.android.demo/.TwitterScreen }
01-24 10:24:02.174: INFO/global(348): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
01-24 10:24:03.594: INFO/ActivityManager(61): Displayed activity nl.gemoro.android.demo/.TwitterScreen: 2412 ms (total 2412 ms)
You have to get the Data from the Twitter service in your onResume()
method
like
@Override
protected void onResume() {
super.onResume();
Uri uri = getIntent().getData();
Uri CALLBACK_URI = Uri.parse("myapp://twitactivity");
if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
String oauth_token=ri.getQueryParameter("oauth_token");
String oauth_verifier= uri.getQueryParameter("oauth_verifier");
Intent i = new Intent(this, YourClass.class); // Go to your activity
startActivity(i);
}
So you get the data from twitter and now you started a new Activity
To get properly call onNewIntent
, you need to specify lauchMode="singleInstance"
or "singleTop"
for activity in the AndroidManifest.xml
file.
I suggest you to use WebView instead of opening it in url.
I have given an answer describing steps for implementing twitter successfully.
精彩评论