如下所示进行一些更改: 1) 对于PrepareRequestTokenActivity Activity 进行更改,如在后台方法中一样
@Override
protected Void doInBackground(Uri...params) {
final Uri uri = params[0];
final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try {
provider.retrieveAccessToken(consumer, oauth_verifier);
final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
//
context.startActivity(new Intent(context,twitter.class));
//executeAfterAccessTokenRetrieval();
Log.i(TAG, "OAuth - Access Token Retrieved");
} catch (Exception e) {
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
}
return null;
}
我在 twitter 类中使用twitter.class作为 twitter 的主类,我的代码如下所示
import oauth.signpost.OAuth;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class twitter extends Activity{
private SharedPreferences prefs;
private final Handler mTwitterHandler = new Handler();
EditText et_sharedata;
Button bt_post,bt_login;
TextView loginstatus,tv_heading;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tw);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
loginstatus=(TextView)findViewById(R.id.tv_twloginstatus);
et_sharedata=(EditText)findViewById(R.id.et_twdata);
tv_heading= (TextView)findViewById(R.id.tv_heading);
tv_heading.setSelected(true);
if(TwitterUtils.isAuthenticated(prefs))
{
}else{
Intent i = new Intent(twitter.this, PrepareRequestTokenActivity.class);
startActivity(i);
}
}
public void onClick(View v){
switch(v.getId()){
case R.id.bt_twshare:{
String s_spin_text = et_sharedata.getText().toString();
try {
TwitterUtils.sendTweet(prefs,s_spin_text);
//TwitterUtils.sendTweet(prefs,getTweetMsg());
mTwitterHandler.post(mUpdateTwitterNotification);
twitter.this.finish();
} catch (Exception ex) {
ex.printStackTrace();
}
}break;
case R.id.iv_back:
twitter.this.finish();
break;
case R.id.iv_index:
startActivity(new Intent(this,home.class));
break;
}
}
@Override
protected void onResume() {
super.onResume();
updateLoginStatus();
}
public void updateLoginStatus() {
if(TwitterUtils.isAuthenticated(prefs))
{
loginstatus.setText("You Are Logged In Twitter");
}else{
loginstatus.setText("You Are Not Logged In Twitter");
}
}
private void clearCredentials() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final Editor edit = prefs.edit();
edit.remove(OAuth.OAUTH_TOKEN);
edit.remove(OAuth.OAUTH_TOKEN_SECRET);
edit.commit();
}
final Runnable mUpdateTwitterNotification = new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), "Tweet sent !", Toast.LENGTH_LONG).show();
}
};
}
而我的tw.xml如下图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/mainbg"
android:layout_gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_strip"
>
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="true"
android:onClick="onClick"
android:src="@drawable/back" />
<TextView
android:id="@+id/tv_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Increase Your Brain Power"
android:ellipsize="marquee"
android:maxWidth="220dp"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="#ffffff"
android:textSize="20px"
/>
<ImageView
android:id="@+id/iv_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="true"
android:onClick="onClick"
android:src="@drawable/index" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:id="@+id/tv_twloginstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="46dp"
android:text=""
android:textColor="#000000" />
<Button
android:id="@+id/bt_twshare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:onClick="onClick"
android:background="@drawable/post_data"/>
</RelativeLayout>
<EditText
android:id="@+id/et_twdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.37"
android:maxLength="140"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="140dp"
android:gravity="top">"
</EditText>
</LinearLayout>