Android Open Source - NewAndroidTwitter Main Activity






From Project

Back to project page NewAndroidTwitter.

License

The source code is released under:

Apache License

If you think the Android project NewAndroidTwitter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.londatiga.android.example;
//  w  ww. jav  a2 s .  c o  m
import net.londatiga.android.twitter.*;
import net.londatiga.android.twitter.oauth.OauthAccessToken;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseActivity {
  private Twitter mTwitter;
  
  public static final String CONSUMER_KEY = "your consumer key";
  public static final String CONSUMER_SECRET = "your consumer secret";
  public static final String CALLBACK_URL = "your callback here";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_main);
    
    mTwitter = new Twitter(this, CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL);
    
    if (mTwitter.sessionActive()) {
      startActivity(new Intent(this, UserActivity.class));
      
      finish();
    } else {
      ((Button) findViewById(R.id.btn_connect)).setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
          signinTwitter();
        }
      });
    }    
  }

  private void signinTwitter() {
    mTwitter.signin(new Twitter.SigninListener() {        
      @Override
      public void onSuccess(OauthAccessToken accessToken, String userId, String screenName) {
        getCredentials();
      }
      
      @Override
      public void onError(String error) {
        showToast(error);
      }
    });
  }
  
  private void getCredentials() {
    final ProgressDialog progressDlg = new ProgressDialog(this);
    
    progressDlg.setMessage("Getting credentials...");
    progressDlg.setCancelable(false);
    
    progressDlg.show();
    
    TwitterRequest request = new TwitterRequest(mTwitter.getConsumer(), mTwitter.getAccessToken());
    
    request.verifyCredentials(new TwitterRequest.VerifyCredentialListener() {
      
      @Override
      public void onSuccess(TwitterUser user) {
        progressDlg.dismiss();
        
        showToast("Hello " + user.name);
        
        saveCredential(user.screenName, user.name, user.profileImageUrl);
        
        startActivity(new Intent(getActivity(), UserActivity.class));
        
        finish();
      }
      
      @Override
      public void onError(String error) {
        progressDlg.dismiss();
        
        showToast(error);
      }
    });
  }
}




Java Source Code List

net.londatiga.android.example.BaseActivity.java
net.londatiga.android.example.MainActivity.java
net.londatiga.android.example.UserActivity.java
net.londatiga.android.twitter.TwitterDialog.java
net.londatiga.android.twitter.TwitterRequest.java
net.londatiga.android.twitter.TwitterUser.java
net.londatiga.android.twitter.Twitter.java
net.londatiga.android.twitter.http.HttpConn.java
net.londatiga.android.twitter.http.HttpParams.java
net.londatiga.android.twitter.http.HttpValues.java
net.londatiga.android.twitter.oauth.OauthAccessToken.java
net.londatiga.android.twitter.oauth.OauthConsumer.java
net.londatiga.android.twitter.oauth.OauthHeader.java
net.londatiga.android.twitter.oauth.OauthProvider.java
net.londatiga.android.twitter.oauth.OauthSignature.java
net.londatiga.android.twitter.oauth.OauthToken.java
net.londatiga.android.twitter.oauth.OauthUtil.java
net.londatiga.android.twitter.util.Base64.java
net.londatiga.android.twitter.util.Cons.java
net.londatiga.android.twitter.util.Debug.java
net.londatiga.android.twitter.util.StringUtil.java
net.londatiga.android.twitter.util.URIUtil.java