我正在尝试创建一个Twitter客户端,我坚持这个场景。欢迎屏幕中有一个按钮,告诉用户授权该应用程序。此按钮将调用twitter授权页面并返回URL。我需要调用onNewIntent方法来捕获URL详细信息并存储它。由于某种原因,该方法未被调用。请帮忙。
android:launchMode="singleTop"
也不起作用。
onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Enabling strict mode
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//get the preferences for the app
nicePrefs = getSharedPreferences("WolfyPref", 0);
//find out if the user preferences are set
if(nicePrefs.getString("user_token", null)==null) {
setContentView(R.layout.activity_main);
//no user preferences so prompt to sign in
niceTwitter = new TwitterFactory().getInstance();
niceTwitter.setOAuthConsumer(TWIT_KEY,TWIT_SECRET);
//try to get request token
try
{
//get authentication request token
niceRequestToken = niceTwitter.getOAuthRequestToken(TWIT_URL);
}
catch(TwitterException te) { Log.e(LOG_TAG, "TE " + te.getMessage()); }
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(this);
}
else
{
//user preferences are set - get timeline
setupTimeline();
}
/* Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
}
的onClick
public void onClick(View v) {
//find view
switch(v.getId()) {
//sign in button pressed
case R.id.fab:
//take user to twitter authentication web page to allow app access to their twitter account
String authURL = niceRequestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
break;
default:
break;
}
}
onNewIntent
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//get the retrieved data
Uri twitURI = intent.getData();
//make sure the url is correct
if(twitURI!=null && twitURI.toString().startsWith(TWIT_URL))
{
//is verifcation - get the returned data
String oaVerifier = twitURI.getQueryParameter("oauth_verifier");
//attempt to retrieve access token
try
{
//try to get an access token using the returned data from the verification page
AccessToken accToken = niceTwitter.getOAuthAccessToken(niceRequestToken, oaVerifier);
//add the token and secret to shared prefs for future reference
nicePrefs.edit()
.putString("user_token", accToken.getToken())
.putString("user_secret", accToken.getTokenSecret())
.apply();
//display the timeline
setupTimeline();
}
catch (TwitterException te)
{ Log.e(LOG_TAG, "Failed to get access token: " + te.getMessage()); }
}
}
由于没有调用onNewIntent方法,我得到了这个屏幕。
答案 0 :(得分:0)
onNewIntent
将针对带有标记' singleTop'的活动进行调用仅当此活动已存在于堆栈顶部时。在你的情况下,你第一次开始你的活动,所以没有onNewIntent
的调用。
有关详细信息,请参阅this。