0
我正在研究一個基本的Android應用程序,在這個應用程序中,我在Twitter上發佈了一個小文本字符串。 問題是,在twitter上發佈我的應用程序退出後。我在之前的應用程序中使用了相同的代碼,但現在它不能正常工作。請讓我知道我在做什麼錯誤。在Android上的Twitter上發佈後應用程序退出?
下面的代碼是PrepareRequestTokenActivity.java
/**
* Prepares a OAuthConsumer and OAuthProvider
*
* OAuthConsumer is configured with the consumer key & consumer secret.
* OAuthProvider is configured with the 3 OAuth endpoints.
*
* Execute the OAuthRequestTokenTask to retrieve the request, and authorize the request.
*
* After the request is authorized, a callback is made here.
*
*/
public class PrepareRequestTokenActivity extends Activity {
public static boolean isTweetCompleted = false;
public static int checkvalue=0;
final String TAG = getClass().getName();
private OAuthConsumer consumer;
private OAuthProvider provider;
private boolean _isProcessCompleted;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.twitter_dialog);
checkvalue=this.getIntent().getIntExtra("check",0);
@SuppressWarnings("unused")
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
try {
this.consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL,Constants.ACCESS_URL,Constants.AUTHORIZE_URL);
} catch (Exception e) {
}
new OAuthRequestTokenTask(this,consumer,provider).execute();
_isProcessCompleted = true;
}
/**
* Called when the OAuthRequestTokenTask finishes (user has authorized the request token).
* The callback URL will be intercepted here.
*/
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
_isProcessCompleted = true;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) {
new RetrieveAccessTokenTask(this,consumer,provider,prefs).execute(uri);
}
}
@Override
protected void onResume() {
if(!_isProcessCompleted)
finish();
else
_isProcessCompleted = false;
super.onResume();
}
public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
private SharedPreferences prefs;
public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) {
this.context = context;
this.consumer = consumer;
this.provider = provider;
this.prefs=prefs;
}
/**
* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret
* for future API calls.
*/
@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, "");
consumer.setTokenWithSecret(token, secret);
//------------------------------------------------------------------------
// context.startActivity(new Intent(context,WeddingRituals.class));
//------------------------------------------------------------------------
executeAfterAccessTokenRetrieval(prefs);
} catch (Exception e) {
}
**PrepareRequestTokenActivity.this.finish();**
return null;
}
}
private void executeAfterAccessTokenRetrieval(SharedPreferences prefs) {
String msg = getIntent().getExtras().getString("tweet_msg");
try {
TwitterUtils.sendTweet(prefs, msg);
Message message = new Message();
message.arg1 = 0;
saveHandler.sendMessage(message);
} catch (Exception e) {
}
}
private Handler saveHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.arg1 == 0){
Toast.makeText(PrepareRequestTokenActivity.this, "Message posted successfully", Toast.LENGTH_LONG).show();
}
}
};
}
LogCat中的任何錯誤跟蹤? – fiddler
logcat沒有錯誤 –
我解決了這個問題,但它只是一個這個時間, 我取消了評論調用返回活動的代碼,我從這裏開始推特。 –