在android中调用Intent.ACTION_VIEW之后返回

时间:2011-07-17 14:38:08

标签: android android-intent twitter-oauth twitter4j

我的应用程序有3个活动A,B,C。活动A调用B.在B中,我调用Intent.ACTION_VIEW对Twitter进行身份验证,如下所示:

public static void DoAuthen(Context context, String CallBackUrl) throws OAuthMessageSignerException, OAuthNotAuthorizedException,
        OAuthExpectationFailedException, OAuthCommunicationException {
    httpOauthConsumer = new CommonsHttpOAuthConsumer(context.getString(R.string.Twitter_ConsumerKey), context
            .getString(R.string.Twitter_ConsumerSecret));
    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);
    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}

验证后,我的应用程序在活动B回叫。这里B呼叫C. 现在如果按下“返回”按钮,它将导航到浏览器(之前用于通过Twitter进行身份验证),而不是B,然后是A. 我该如何解决这个问题?

3 个答案:

答案 0 :(得分:8)

请参阅Tasks and Back stack in android。您可以在应用程序中使用两个任务 - 第一个是您开展业务,第二个是授权。您使用意图标记FLAG_ACTIVITY_NEW_TASK开始授权并使用参数android:clearTaskOnLaunch。祝你好运!

答案 1 :(得分:1)

我在ACTION_VIEW意图中添加了以下标志,它解决了回到浏览器问题

consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
                    "http://twitter.com/oauth/access_token",
                    "http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL);
Intent oauthIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));        
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
oauthIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);

答案 2 :(得分:0)

在C中你可以覆盖后退按钮直接进入B

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        startActivity(C.this,B.class);
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}