在Twitter上发布Android后申请退出?

时间:2012-08-29 15:24:03

标签: android twitter

我正在开发一个基本的Android应用程序,我在我的个人资料上在twitter上发布一个小文本字符串。 问题是,在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();
            }
        }
    };

}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,但这次只是一个问题,我取消评论调用后来活动的代码,我来自twitter

context.startActivity(new Intent(context,WeddingRituals.class));