开发者

Oauth Connectivity for twitter

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();
}

I use the above Code for making connection for twitter but it only works for one time if I want another time for connection then it never provide me second time connection.

Thankx


Could this be an Activity lifecycle issue? Your call to getTwitter() occurs in onCreate, which only gets called when the Activity is created. If a user navigates away then comes back to your app, it may still be running, so onCreate would not get called again. Have a look at the Activity lifecycle, and add some debug code to each of the lifecycle methods (onResume, onPause etc) to get an idea of when they are called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜