开发者

callback from OAuth not call onResume()

I am using oauth-signpost as my OAuth library. When I direct the user to the authorization page, the user logs in and can authorize my application. When that happens, the application is returned to focus and starts the onCreate() method instead of the onResume() method.

My code as follows:

private static final String             CONSUMER_KEY      = "---";
private static final String             CONSUMER_SECRET   = "---";

private static String                   ACCESS_KEY        = null;
private static String                   ACCESS_SECRET     = null;

private static final String             REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
private static final String             ACCESS_TOKEN_URL  = "https://api.twitter.com/oauth/access_token";
private static final String             AUTH_URL          = "https://api.twitter.com/oauth/authorize";
private static final String             CALLBACK_URL      = "myApp://Tweets";

private static CommonsHttpOAuthConsumer consumer          = new CommonsHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);

private static CommonsHttpOAuthProvider provider          = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL,AUTH_URL);


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String authUrl = null;
    try {
        authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
    } catch(OAuthMessageSignerException e) {
        e.printStackTrace();
    } catch(OAuthNotAuthorizedException e) {
        e.printStackTrace();
    } catch(OAuthExpectationFailedException e) {
        e.printStackTrace();
    } catch(OAuthCommunicationException e) {
        e.printStackTrace();
    }
    Log.d("OAuthTwitter", "authUrl" + authUrl);

    WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setVisibility(View.VISIBLE);
    setContentView(webview);
    webview.loadUrl(authUrl);

}

    @Override
public void onResume() {
    super.onResume();
    Uri uri = this.getIntent().getData();

    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        Log.d("OAuthTwitter", uri.toString());
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d("OAuthTwitter", verifier);
        try {

            provider.retrieveAccessToken(consumer, verifier);
            ACCESS_KEY = consumer.getToken();
            ACCESS_SECRET = consumer.getTokenSecret();

            Log.d("OAuthTwitter", ACCESS_KEY);
            Log.d("OAuthTwitter", ACCESS_SECRET);

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
    }

Manifest.xml

    <application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    <activity
        android:name=".Tweets"
        android:label="@string/app_name">
        <intent-filter>
     开发者_如何学Python       <action
                android:name="android.intent.action.MAIN" />
            <category
                android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="Tweets" />

        </intent-filter>

    </activity>

How do I get my application to call onResume() so that I can then continue with the OAuth process?


In this case, try setting your activity launch mode to singleTask.

See the link for more information: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜