Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.slk.TwitterSample;
- import java.io.IOException;
- import com.twitterapime.rest.Credential;
- import com.twitterapime.rest.TweetER;
- import com.twitterapime.rest.UserAccountManager;
- import com.twitterapime.search.LimitExceededException;
- import com.twitterapime.search.Tweet;
- import com.twitterapime.xauth.Token;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class TwitterSample extends Activity {
- UserAccountManager m;
- Credential c;
- Context context;
- String userName="",pwd="";
- private String CONSUMER_KEY = "fPki8w1t1C7Hn92gZ43VkQ";
- private String CONSUMER_SECRET = "MUl2Etxbq1UVAJdUVNexZ00doYtZiZwhXVCFyw8EBc";
- public static String TWITTER_USERKEY = "userKey";
- public static String TWITTER_USERSECRET = "userSecret";
- public static String TWITTER_USERNAME = "username";
- public static String TWITTER_PASSWORD = "password";
- public static String SCREEN_NAME = "userName";
- public String key,secret;
- private static final int FPASS_DIALOG = 1;
- private static final int ERROR_IN_INPUT = 2;
- SharedPreferences TwitterPref;
- String userKey,userSecret;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TwitterPref = this.getSharedPreferences("TwitterPref", MODE_WORLD_READABLE);
- key = TwitterPref.getString(TwitterSample.TWITTER_USERKEY,"");
- secret = TwitterPref.getString(TwitterSample.TWITTER_USERSECRET, "");
- userName = TwitterPref.getString(TwitterSample.TWITTER_USERNAME, "");
- pwd = TwitterPref.getString(TwitterSample.TWITTER_PASSWORD, "");
- if(key.equals("") && key.length()<=0 || secret.equals("") && secret.length()<=0){
- showDialog(1);
- c = new Credential(userName,pwd,CONSUMER_KEY,CONSUMER_SECRET);
- Log.d("Details",userName+":"+pwd);
- m = UserAccountManager.getInstance(c);
- try {
- if (m.verifyCredential()) {
- Token t1=m.getAccessToken();
- userKey =t1.getToken();
- userSecret = t1.getSecret();
- Log.d("Keys",userKey+":"+userSecret);
- // SAVE IN PREFERENCE
- TwitterPref = TwitterSample.this.getSharedPreferences("TwitterPref", MODE_WORLD_READABLE);
- SharedPreferences.Editor TwitterEditor = TwitterPref.edit();
- TwitterEditor.putString(TwitterSample.TWITTER_USERKEY, userKey);
- TwitterEditor.putString(TwitterSample.TWITTER_USERSECRET, userSecret);
- TwitterEditor.putString(TwitterSample.TWITTER_USERNAME, userName);
- TwitterEditor.putString(TwitterSample.TWITTER_PASSWORD, pwd);
- TwitterEditor.commit();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LimitExceededException e) {
- e.printStackTrace();
- }
- }
- c = new Credential(userName,pwd,CONSUMER_KEY,CONSUMER_SECRET);
- m = UserAccountManager.getInstance(c);
- try {
- if (m.verifyCredential()) {
- Tweet t = new Tweet("RT @test "+"Hello how r u");
- TweetER ter = TweetER.getInstance(m);
- try {
- t = ter.post(t);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (LimitExceededException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LimitExceededException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected Dialog onCreateDialog(int select) {
- switch(select) {
- case FPASS_DIALOG:
- {
- LayoutInflater factory = LayoutInflater.from(this);
- final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
- final EditText username = (EditText)textEntryView.findViewById(R.id.username_edit);
- final EditText password = (EditText)textEntryView.findViewById(R.id.password_edit);
- return new AlertDialog.Builder(TwitterSample.this)
- .setIcon(R.drawable.alert_dialog_icon)
- .setTitle(R.string.alert_dialog_text_entry)
- .setView(textEntryView)
- .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- /* User clicked OK so do some stuff */
- userName= username.getText().toString();
- pwd = password.getText().toString();
- dialog.cancel();
- }
- })
- .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.cancel();
- /* User clicked cancel so do some stuff */
- }
- })
- .create();
- }
- case ERROR_IN_INPUT:
- {
- return new AlertDialog.Builder(TwitterSample.this)
- .setTitle("Error in input").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton)
- {
- showDialog(FPASS_DIALOG);
- }
- }).create();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement