我有一个关于登录twitter的活动,当我直接运行它时工作正常但是当我从输入法服务调用它时它不起作用...为什么?以下是我的活动代码:
public class PostTwitter extends Activity {
private Button btnLogin;
/**
* Register your here app https://dev.twitter.com/apps/new and get your
* consumer key and secret
* */
static String TWITTER_CONSUMER_KEY = "CfyZdyVYFeZ34nlHkjjYmHiVk";
static String TWITTER_CONSUMER_SECRET = "QolQok2bKohLBwvctQHn1cvECQbrwWNjgrUxyoi6XgtSzHj1Gu";
// Preference Constants
static String PREFERENCE_NAME = "twitter_oauth";
static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";
private static final String PREF_USER_NAME = "twitter_user_name";
static final String TWITTER_CALLBACK_URL = "oauth://t4jsample_3";
// Twitter oauth urls
static final String URL_TWITTER_AUTH = "auth_url";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
// Twitter
private static Twitter twitter;
private static RequestToken requestToken;
// Shared Preferences
private static SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_twitter);
btnLogin = (Button) findViewById(R.id.btnLogoutTwitter);
// Shared Preferences
mSharedPreferences = getApplicationContext().getSharedPreferences(
"MyPref", 0);
/**
* Twitter login button click event will call loginToTwitter() function
* */
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (isTwitterLoggedInAlready()) {
logoutFromTwitter();
} else {
new LoginNewUser().execute();
}
}
});
}
/**
* Function to update status
* */
class LoginNewUser extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
// Tell twitter4j that we want to use it with our app
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setDebugEnabled(true);
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
PostTwitter.this.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
}
@Override
protected void onResume() {
super.onResume();
Log.i("TAG", "Arrived at onResume");
dealWithTwitterResponse();
}
/**
* Twitter has sent us back into our app</br> Within the intent it set back
* we have a 'key' we can use to authenticate the user
*
* @param intent
*/
private void dealWithTwitterResponse() {
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
// If the// user// has// just// logged// in
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
Log.d("DEBUG", "loggged in");
new SaveTokenAfterLogin().execute(oauthVerifier);
// authoriseNewUser(oauthVerifier);
}
else
{
Log.d("DEBUG", "Not logged in");
}
}
/**
* Create an access token for this new user</br> Fill out the Twitter4j
* helper</br> And save these credentials so we can log the user straight in
* next time
*
* @param oauthVerifier
*/
class SaveTokenAfterLogin extends AsyncTask<String, Void, AccessToken> {
@Override
protected AccessToken doInBackground(String... oauthVerifier) {
// TODO Auto-generated method stub
// Log.d("DEBUG",oauthVerifier[0]);
try {
AccessToken at = twitter.getOAuthAccessToken(requestToken,
oauthVerifier[0]);
twitter.setOAuthAccessToken(at);
return at;
} catch (TwitterException e) {
// Toast.makeText(this,
// "Twitter auth error x01, try again later",
// Toast.LENGTH_SHORT).show();
}
return null;
}
protected void onPostExecute(AccessToken at) {
Log.d("DEBUG", "in post");
String token = at.getToken();
String secret = at.getTokenSecret();
Editor editor = mSharedPreferences.edit();
// editor.putString(PREF_ACCESS_TOKEN, token);
// editor.putString(PREF_ACCESS_TOKEN_SECRET, secret);
// editor.commit();
editor.putString(PREF_KEY_OAUTH_TOKEN, token);
editor.putString(PREF_KEY_OAUTH_SECRET, secret);
editor.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
// e.putString(PREF_USER_NAME, username);
editor.commit();
}
}
/**
* Check user already logged in your application using twitter Login flag is
* fetched from Shared Preferences
* */
private boolean isTwitterLoggedInAlready() {
// return twitter login status from Shared Preferences
return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
}
/**
* Function to logout from twitter It will just clear the application shared
* preferences
* */
private void logoutFromTwitter() {
// Clear the shared preferences
Editor e = mSharedPreferences.edit();
e.remove(PREF_KEY_OAUTH_TOKEN);
e.remove(PREF_KEY_OAUTH_SECRET);
e.remove(PREF_KEY_TWITTER_LOGIN);
e.commit();
btnLogin.setText("Login");
Toast.makeText(getApplicationContext(), "Successfully LogOut",
Toast.LENGTH_SHORT).show();
}
//
}
在我的输入法服务中,我就这样称呼它
twitter_post.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Redirect to dashboard / home screen.
oneTouchPopupDialog.dismiss();
// make_twitter_post();
Intent i = new Intent(getBaseContext(), PostTwitter.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
活动是开放的,但登录不起作用....任何人都知道有任何其他代码嵌入从服务调用吗?提前谢谢