【发布时间】: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