推特的Oauth Connectivity

时间:2011-02-10 06:10:16

标签: android twitter oauth

private static  String CONSUMER_KEY = "mrnCC41nxtwkdFAmToEhtg";
private static final String CONSUMER_SECRET = "kmmVuahEspGvdl14aCD1GSBZpeHbxvkpAez7aKaaQ";
EditText editPinCode;
LinearLayout lin;              
public Logger slr;
LinearLayout container;

public LoginT(){

}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.twitter);
    editPinCode = new EditText(this);
    lin = (LinearLayout)findViewById(R.id.LinearLayout01);
    handleEvent = new Handler();
    twitterConnection = new TwitterFactory().getInstance();
    context = this;     
    oHelper = new OAuthHelp(this);
    getTwitter(context); 
}

/**
 * Connects to twittter    
 * @param v                
 */
public void getTwitter(Context ctx) {  //updated code
    handleEvent.post(new Runnable() {
        //          handleEvent.postAtFrontOfQueue(new Runnable() {


        public void run() {                   
            if (oHelper.hasAccessToken()) 
            {
                Log.e("run if","run");
                oHelper.configureOAuth(twitterConnection);
                try 
                {
                    i=i+1;
                    Log.e("run try","run");
                    twitterConnection.updateStatus(Calendar.MINUTE+i+"Hi this is Arun......");
                    //twitterConnection.se
                    Log.e("finish","start");
                    finish();
                    Log.e("finish","end");
                    } 
                catch (TwitterException e) 
                {
                    Log.d("TWEET", "Error Updating status " + e.getMessage());
                    e.printStackTrace();
                }
            } 
            else 
            {
                Log.e("run else","run");
                try {        
                    twitterConnection.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
                    requestToken = twitterConnection.getOAuthRequestToken("");
                    Log.e("REQUEST_TOKEN",requestToken+"");
                    webViewDialog(requestToken.getAuthorizationURL(), 0);
                } 
                catch (TwitterException e) 
                {             
                    e.printStackTrace();
                }    
            }

        }});

}           

/**
 * Shows Dialog for authentications
 *
 * @param authorizationURL
 * @param type
 */
private void webViewDialog(final String authorizationURL, final int type) {
    Log.e("webViewDialog","webViewDialog");
    container = new LinearLayout(this);
    container.setMinimumWidth(200);
    container.setMinimumHeight(320);
    webView = new WebView(this);
    webView.setMinimumWidth(200);
    webView.setMinimumHeight(380);
    webView.getSettings().setJavaScriptEnabled(true);

    //      webView.dispatchWindowFocusChanged(true);
    webView.setWebViewClient(new MyWebViewClient(this,LoginT.this));
    webView.loadUrl(authorizationURL);
    container.addView(webView); 
    lin.addView(container);

    //      Builder webDialog = new AlertDialog.Builder(this);
    //      webDialog.setView(container).setTitle("Twitter Client").setCancelable(true)
    //      .show();
}

/**    
 * Pin code dialog Requests the user to enter pin shown on twitter
 */
public void twitterPinCodeDialog() {

    try {                 
        //                       accessToken = twitterConnection.getOAuthAccessToken(requestToken,ss);
        try{
            accessToken = twitterConnection.getOAuthAccessToken(requestToken);
        }
        catch(Exception e1){
            Log.w("Excep  e1",e1+"");
        }
        oHelper.storeAccessToken(accessToken); 
        Log.w("ohelper",oHelper.toString());
        twitterConnection.updateStatus("Tweeted Successfully"+new Date().toString()); 
        Log.e(" ","2    "+accessToken);
        Log.e(" ","3");

        webView.destroy();
        webView.removeAllViews();
        container.removeAllViews();
        this.finish();

        //                      Log.i("Access Token:", accessToken.getToken());
        //                      Log.i("Access Secret:", accessToken.getTokenSecret());

    } catch (TwitterException te) {  
        oHelper.storeAccessToken(accessToken);                                      
        try {
            twitterConnection.updateStatus("HI.... ");
        } catch (TwitterException e) {
            e.printStackTrace();
        }     

    }  
}

@Override
protected Dialog onCreateDialog(int id)
{   
    switch (id) {
    //      case DIALOG_LOADING: 
    //      {
    //          //              dialog = new ProgressDialog(this);
    //          dialog.setMessage("Please wait while loading...");
    //          dialog.setIndeterminate(true);
    //          dialog.setCancelable(true);
    //          return dialog;
    //      }
    }   
    return null;             
} 

//      
//      @Override
public void dismiss() {
    Log.w("dismiss","dismiss");
    try{
        //          webView.destroy();
        //          webView.removeAllViews();
        //          container.removeAllViews();
        //          this.finish();
        System.exit(0);
    }catch(Exception e){
        e.printStackTrace();
    }
}
//      
@Override
public boolean onSearchRequested() {
    Log.e("Search","Search");
    return super.onSearchRequested();
}

我使用上面的代码为twitter建立连接,但是如果我想再次连接那么它只能工作一次,那么它永远不会给我第二次连接。

Thankx

1 个答案:

答案 0 :(得分:0)

这可能是Activity生命周期问题吗?您对getTwitter()的调用发生在onCreate,只有在创建Activity时才会调用。如果用户导航然后返回到您的应用,它可能仍在运行,因此不会再次调用onCreate。查看Activity lifecycle,并为每个生命周期方法(onResumeonPause等)添加一些调试代码,以了解它们何时被调用。