我需要创建twitter fabric可重用组件。我可以通过简单地从类中调用方法来允许使用twitter登录。
代码
类
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterAuthToken;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.core.services.StatusesService;
import io.fabric.sdk.android.Fabric;
public class TwitterAuth {
private String CONSUMER_KEY;
private String CONSUMER_SECRET;
private Context context;
private TwitterAuthClient client;
private StatusesService service;
public TwitterAuth(Context context, String CONSUMER_KEY, String CONSUMER_SECRET) {
this.CONSUMER_KEY = CONSUMER_KEY;
this.CONSUMER_SECRET = CONSUMER_SECRET;
this.context = context;
configureKey();
}
public void configureKey() {
TwitterAuthConfig authConfig = new TwitterAuthConfig(CONSUMER_KEY, CONSUMER_SECRET);
Fabric.with(context, new Twitter(authConfig));
}
public void doLogin() {
client = new TwitterAuthClient();
client.authorize((Activity) context, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
final TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
String userName = session.getUserName();
Toast.makeText(context, "TWITTER EASY LIB TEST :: Done Login With \n Username :" + userName + " \n Token :" + token + "\n Secret :" + secret, Toast.LENGTH_LONG).show();
//Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(TwitterException e) {
Toast.makeText(context, "TWITTER EASY LIB TEST :: failure", Toast.LENGTH_SHORT).show();
}
});
}
public void doLogout() {
Twitter.getSessionManager().clearActiveSession();
}
public void publishTweet(String tweet) {
service = Twitter.getInstance().getApiClient().getStatusesService();
service.update(tweet, null, null, null, null, null, null, null, new Callback<Tweet>() {
@Override
public void success(Result<Tweet> tweetResult) {
Toast.makeText(context, "Tweet Updated !",
Toast.LENGTH_SHORT).show();
}
@Override
public void failure(TwitterException e) {
Toast.makeText(context, "Error occured !",
Toast.LENGTH_SHORT).show();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
client.onActivityResult(requestCode, resultCode, data);
}
}
活动
package codelynks.twitter.twitterintegration;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import com.easytweet.TwitterAuth;
public class CheckLib extends ActionBarActivity {
private Button cus;
private TwitterAuth auth;
private String CONSUMER_KEY = "", CONSUMER_SECRET = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = new TwitterAuth(CheckLib.this, CONSUMER_KEY, CONSUMER_SECRET);
setContentView(R.layout.activity_main);
cus = (Button) findViewById(R.id.cusbutton);
cus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
auth.doLogin();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
auth.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
这里我将得到回调方法的结果
public void success(Result<TwitterSession> twitterSessionResult) {}
**or**
public void failure(TwitterException e) {}
如何将此结果(SUCCESS / FAILURE)传递给活动CheckLib以执行进一步操作。?
任何帮助将不胜感激:)
答案 0 :(得分:7)
您可以在TwitterAuth.class
中设置成功或失败的监听器,然后在您的活动(CheckLib.class
)中设置此监听器,以便在成功或失败时通知您,如下所示:
public class TwitterAuth {
private TwitterLoginListener listener;
public void setListener( TwitterLoginListener listener){
this.listener = listener;
}
Interfase TwitterLoginListener{
public void success(Result<TwitterSession> twitterSessionResult);
public void failure(TwitterException e);
}
.
.
.
在成功和失败方法中,您需要填写听众:
成功方法(在TwitterAuth.class中):
if(listener != null){
listener.success(twitterSessionResult);
}
失败方法(在TwitterAuth.class中):
if(listener != null){
listener.failure(e);
}
然后在你的活动集听众中:
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = new TwitterAuth(CheckLib.this, CONSUMER_KEY, CONSUMER_SECRET);
auth.setListener(new TwitterLoginListener{
@Override
public void success(Result<TwitterSession> twitterSessionResult){
//login success
}
@Override
public void failure(TwitterException e){
//login failed
}
});
.
.
.
答案 1 :(得分:1)
如果它是基本类型,如布尔值或字符串(好的,这个不是原始的,但仍然是),你可以将它作为Extra传递给你发送给活动的Intent。
如果它是一个更复杂的对象,或者您的班级没有Context
访问权限,请尝试使用greenrobot EventBus,这是一个非常酷的库,专门为这种情况创建。
答案 2 :(得分:0)
You can use interface and implement the interface method from your dologin method
check my sample
public interface sampleInterface {
// you can define any parameter as per your requirement
public void yourMethod(boolean value);
}
public void doLogin(sampleInterface si) {
public void publishTweet(String tweet) {
sampleInterface sampleIn;
service = Twitter.getInstance().getApiClient().getStatusesService();
service.update(tweet, null, null, null, null, null, null, null, new Callback<Tweet>() {
@Override
public void success(Result<Tweet> tweetResult) {
Toast.makeText(context, "Tweet Updated !",
Toast.LENGTH_SHORT).show();
si.yourMethod(true);
}
@Override
public void failure(TwitterException e) {
Toast.makeText(context, "Error occured !",
Toast.LENGTH_SHORT).show();
si.yourMethod(false);
}
});
}
}
inside your activity class
public void onClick(View v) {
auth.doLogin(new sampleInterface() {
@Override
public void yourMethod(boolean value) {
//GET your result
}
});
}