【问题标题】:How to remove browser from background after twitter authentication?Twitter身份验证后如何从后台删除浏览器?
【发布时间】:2014-10-31 16:41:54
【问题描述】:

在我的 android 应用程序中,我们使用 Scribe API 进行 Twitter 登录。使用以下代码在浏览器中调用 twitter 身份验证 url

OAuthService service  = new ServiceBuilder()
    .provider(TwitterApi.SSL.class)
    .apiKey(myApiKey)
    .apiSecret(myApiSecret)
    .callback(twitterCallback)
    .build();
Token  token = service.getRequestToken();
String authUrl = service.getAuthorizationUrl(token);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setData(Uri.parse(authUrl));
context.startActivity(intent);

在完成 twitter 身份验证后返回活动,现在我们正在转移到应用程序中的另一个活动,即 MainActivity。

@Override
protected void onResume() {
super.onResume();
if(uri != null && uri.toString().startsWith(“oauth://twitter”){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } 
}

清单代码如下

<activity
        android:name=".LauncherActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <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:host="twitter"
                android:scheme="oauth" />
        </intent-filter>
    </activity>

在主要活动中,我们有注销按钮,当我在这里按下注销时调用下面的方法,

private void logout(){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish();
}

到目前为止工作正常,现在进入设备主屏幕。这里的问题是当我长按显示两个应用程序的主页按钮时,一个是我的本机应用程序,另一个是浏览器中的 myapp。认证完成后如何处理删除浏览器?

提前致谢

【问题讨论】:

  • 得到的解决方案只是在浏览器中打开之前向意图添加了一个标志。 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS。现在它的工作完美

标签: android twitter twitter-oauth flags scribe


【解决方案1】:

终于找到解决方案,在 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 中打开之前在 Intent 中添加一个标志。现在它的工作完美。代码是

   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ); 
   intent.setData(Uri.parse(result));
   context.startActivity(i);

Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 参考这里http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

【讨论】:

    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2020-10-22
    • 2014-04-04
    • 2016-12-14
    • 2019-04-06
    • 1970-01-01
    相关资源
    最近更新 更多