2016-07-19 46 views
-2

我正在嘗試創建一個twitter客戶端,並且我被這種情況卡住了。歡迎屏幕上有一個按鈕,用於指示用戶授權應用程序。此按鈕將調用Twitter授權頁面並返回一個URL。我需要調用onNewIntent方法來捕獲URL的詳細信息並將其存儲。由於某種原因,該方法沒有被調用。請幫忙。onNewIntent方法沒有被調用

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()); } 

    } 
} 

由於onNewInte nt方法沒有被調用我得到這個屏幕。

enter image description here

回答

0

onNewIntent將調用與標誌活動「singleTop」僅當此活動已經存在於堆棧中的頂部。在你的情況下,你第一次開始你的活動,所以沒有調用onNewIntent

查看this瞭解更多信息。

+0

我應該在這裏做什麼來解決這個問題? – sreeragnk

+0

如果您有WelcomeActivity和AuthorizeActivity,只需從意圖開始,並通過「額外」傳遞所有數據。然後在onCreate方法的第二個活動中檢索extras並執行任何你想要的操作。 –

+0

之前的代碼工作正常。我做了一些事情,現在不能工作了。我無法添加更多類文件。必須使用相同的代碼。 – sreeragnk

相關問題