我正在使用Fabric将Android集成到Android应用程序中。
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。
发布推文后,我得到的屏幕如下,不会重定向到应用。
注意:登录时效果很好..
感谢。
答案 0 :(得分:2)
不确定你是否遇到了同样的问题,但在我的情况下,我得到的是onActivityResult()回调,但是,尽管推文已经成功发布,但结果编号不是RESULT_OK