2011-02-10 61 views
0
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的連接,但它只適用於一次,如果我想再次連接,那麼它永遠不會給我第二次連接。Oauth連接的推特

謝謝x

+0

如果您最初提供了實際值,您將需要重置消費者密鑰/密鑰。 – abraham 2011-02-11 01:20:24

回答

0

難道這是一個Activity生命週期問題?您致電getTwitter()發生在onCreate,只有在創建Activity時纔會調用。如果用戶導航離開,然後回到您的應用程序,它可能仍在運行,所以onCreate不會再被調用。查看Activity lifecycle,併爲每個生命週期方法(onResume,等)添加一些調試代碼,以瞭解它們何時被調用。

+0

行..我會嘗試你的建議............... – AndroidDanger 2011-02-14 06:05:14