2016-04-04 79 views
2

我正在使用Fabric來將Twitter整合到Android應用程序中。使用TweetComposer發送Tweet時的回叫?

public class MainActivity extends AppCompatActivity { 

    private static final String TWITTER_KEY = ""; 
    private static final String TWITTER_SECRET = ""; 
    private TwitterLoginButton loginButton; 
    private Button btnPostTweet; 
    private static final int TWEET_COMPOSER_REQUEST_CODE = 100; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); 
     Fabric.with(this, new Twitter(authConfig)); 
     setContentView(R.layout.activity_main); 

     loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); 
     btnPostTweet = (Button) findViewById(R.id.btn_post_tweet); 
     btnPostTweet.setOnClickListener(onClickListener); 
     loginButton.setCallback(new Callback<TwitterSession>() { 
      @Override 
      public void success(Result<TwitterSession> result) { 
      Twitter.getInstance().core.getSessionManager().getActiveSession() 
       TwitterSession session = result.data; 
       String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")"; 
       Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
      } 

      @Override 
      public void failure(TwitterException exception) { 
       Log.d("TwitterKit", "Login with Twitter failure", exception); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     // Make sure that the loginButton hears the result from any 
     // Activity that it triggered. 
     if (requestCode == TWEET_COMPOSER_REQUEST_CODE && resultCode == RESULT_OK) 
      Toast.makeText(MainActivity.this, "Updated tweet using composer", Toast.LENGTH_SHORT).show(); 
     else 
      loginButton.onActivityResult(requestCode, resultCode, data); 
    } 

    private View.OnClickListener onClickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 
       case R.id.btn_post_tweet: 
        postTweet(); 
//     postTweetManually(); 
        break; 
       default: 
        break; 
      } 
     } 
    }; 

    private void postTweet() { 
     /* TweetComposer.Builder builder = new TweetComposer.Builder(this) 
       .text("just setting up my Fabric."); 
     Intent twitterIntent = builder.createIntent(); 
     startActivityForResult(twitterIntent, REQUEST_TWEET_POST);*/ 

     Intent intent = null; 
     try { 
      intent = new TweetComposer.Builder(this) 
        .text("Tweet from Fabric!") 
        .url(new URL("http://www.twitter.com")) 
        .createIntent(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     startActivityForResult(intent, TWEET_COMPOSER_REQUEST_CODE); 
    } 

    private void postTweetManually() { 
     TwitterSession twitterSession = Twitter.getSessionManager().getActiveSession(); 
     StatusesService statusesService = Twitter.getApiClient(twitterSession).getStatusesService(); 
     String username = Twitter.getSessionManager().getActiveSession().getUserName(); 
     statusesService.update("@" + username + "Manually update on twitter1", 1L, true, 0.0d, 0.0d, "", true, true, new Callback<Tweet>() { 
      @Override 
      public void success(Result<Tweet> result) { 
       Toast.makeText(MainActivity.this, "Tweet Updated", Toast.LENGTH_LONG).show(); 
       Log.d("Tweet Updated", result.data.user.name); 
      } 

      @Override 
      public void failure(TwitterException e) { 
       Log.d("Tweet Update Failed", e.getMessage()); 
      } 
     }); 
    } 
} 

我還沒有在我的設備中安裝Twitter應用程序。

所以TwitterComposer打開WebBroswer。

發佈tweet後,我得到像下面這樣的屏幕,不重定向到應用程序。

enter image description here

注意:當登錄它的作品完美..

感謝。

+0

你可以分享你的解決方案?在登錄時它的意思是什麼意思? – 2MuchSmoke

+0

您可以使用webview來在發送關鍵字 「完成」之後過濾url,並在未安裝本機應用程序的情況下創建自己的回調功能。 –

回答

2

不知道你是否有同樣的問題,我有但對我來說,我得到onActivityResult()回調,但resulCode沒有RESULT_OK雖然鳴叫已成功發佈

+0

那麼在你的情況下'onActivityResult'被調用? – AndiGeeky

+0

確實如此,但resultCode不正確 –