返回列表 发帖

[实例] Android好源码 AndTweet 看了绝不后悔

这个网上一个比较有名的twitter项目AndTweet,里面有很多值得学习的地方,没钱丢上来混钱了, 有钱的捧个钱场, 没钱的捧个人场。

部分代码:
设置
  1. public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceChangeListener {

  2.         private static final String TAG = "AndTweetPreferences";

  3.         private static final int DIALOG_AUTHENTICATION_FAILED = 1;
  4.         private static final int DIALOG_CHECKING_CREDENTIALS = 2;
  5.         private static final int DIALOG_SERVICE_UNAVAILABLE = 3;
  6.         private static final int DIALOG_CONNECTION_TIMEOUT = 4;

  7.         public static final String INTENT_RESULT_KEY_AUTHENTICATION = "authentication";

  8.         public static final String KEY_TWITTER_USERNAME = "twitter_username";
  9.         public static final String KEY_TWITTER_PASSWORD = "twitter_password";
  10.         public static final String KEY_HISTORY_SIZE = "history_size";
  11.         public static final String KEY_HISTORY_TIME = "history_time";
  12.         public static final String KEY_FETCH_FREQUENCY = "fetch_frequency";
  13.         public static final String KEY_AUTOMATIC_UPDATES = "automatic_updates";
  14.         public static final String KEY_RINGTONE_PREFERENCE = "notification_ringtone";
  15.         //public static final String KEY_EXTERNAL_STORAGE = "storage_use_external";

  16.         public static final int MSG_ACCOUNT_VALID = 1;
  17.         public static final int MSG_ACCOUNT_INVALID = 2;
  18.         public static final int MSG_SERVICE_UNAVAILABLE_ERROR = 3;
  19.         public static final int MSG_CONNECTION_EXCEPTION = 4;
  20.         public static final int MSG_SOCKET_TIMEOUT_EXCEPTION = 5;

  21.         private CheckBoxPreference mAutomaticUpdates;
  22.         //private CheckBoxPreference mUseExternalStorage;
  23.         private ListPreference mHistorySizePreference;
  24.         private ListPreference mHistoryTimePreference;
  25.         private ListPreference mFetchFrequencyPreference;
  26.         private EditTextPreference mEditTextUsername;
  27.         private EditTextPreference mEditTextPassword;
  28.         private RingtonePreference mNotificationRingtone;

  29.         private ProgressDialog mProgressDialog;

  30.         protected boolean mAuthentiated = false;

  31.         @Override
  32.         protected void onCreate(Bundle savedInstanceState) {
  33.                 super.onCreate(savedInstanceState);
  34.                 addPreferencesFromResource(R.xml.preferences);
  35.                 mHistorySizePreference = (ListPreference) getPreferenceScreen().findPreference(KEY_HISTORY_SIZE);
  36.                 mHistoryTimePreference = (ListPreference) getPreferenceScreen().findPreference(KEY_HISTORY_TIME);
  37.                 mFetchFrequencyPreference = (ListPreference) getPreferenceScreen().findPreference(KEY_FETCH_FREQUENCY);
  38.                 mAutomaticUpdates = (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_AUTOMATIC_UPDATES);
  39.                 mNotificationRingtone = (RingtonePreference) getPreferenceScreen().findPreference(KEY_RINGTONE_PREFERENCE);
  40.                 mEditTextUsername = (EditTextPreference) getPreferenceScreen().findPreference(KEY_TWITTER_USERNAME);
  41.                 mEditTextPassword = (EditTextPreference) getPreferenceScreen().findPreference(KEY_TWITTER_PASSWORD);
  42.                 if (mEditTextUsername.getText() == null || mEditTextPassword.getText() == null || mEditTextUsername.getText().length() == 0 || mEditTextPassword.getText().length() == 0) {
  43.                         mAutomaticUpdates.setEnabled(false);
  44.                 }
  45.                 mNotificationRingtone.setOnPreferenceChangeListener(this);
  46.                 /*
  47.                 mUseExternalStorage = (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_EXTERNAL_STORAGE);
  48.                 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  49.                         mUseExternalStorage.setEnabled(false);
  50.                         mUseExternalStorage.setChecked(false);
  51.                 }
  52.                 */
  53.                 updateFrequency();
  54.                 updateHistorySize();
  55.                 updateHistoryTime();
  56.                 updateRingtone(getPreferenceScreen().getSharedPreferences().getString(KEY_RINGTONE_PREFERENCE, null));
  57.         }

  58.         @Override
  59.         protected void onResume() {
  60.                 super.onResume();
  61.                 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  62.         }

  63.         @Override
  64.         protected void onPause() {
  65.                 super.onPause();
  66.                 getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  67.         }

  68.         protected void updateHistorySize() {
  69.                 String[] k = getResources().getStringArray(R.array.history_size_keys);
  70.                 String[] d = getResources().getStringArray(R.array.history_size_display);
  71.                 String displayHistorySize = d[0];
  72.                 String historySize = mHistorySizePreference.getValue();
  73.                 for (int i = 0; i < k.length; i++) {
  74.                         if (historySize.equals(k[i])) {
  75.                                 displayHistorySize = d[i];
  76.                                 break;
  77.                         }
  78.                 }
  79.                 MessageFormat sf = new MessageFormat(getText(R.string.summary_preference_history_size).toString());
  80.                 mHistorySizePreference.setSummary(sf.format(new Object[] { displayHistorySize }));
  81.         }

  82.         protected void updateHistoryTime() {
  83.                 String[] k = getResources().getStringArray(R.array.history_time_keys);
  84.                 String[] d = getResources().getStringArray(R.array.history_time_display);
  85.                 String displayHistoryTime = d[0];
  86.                 String historyTime = mHistoryTimePreference.getValue();
  87.                 for (int i = 0; i < k.length; i++) {
  88.                         if (historyTime.equals(k[i])) {
  89.                                 displayHistoryTime = d[i];
  90.                                 break;
  91.                         }
  92.                 }
  93.                 MessageFormat sf = new MessageFormat(getText(R.string.summary_preference_history_time).toString());
  94.                 mHistoryTimePreference.setSummary(sf.format(new Object[] { displayHistoryTime }));
  95.         }

  96.         protected void updateFrequency() {
  97.                 String[] k = getResources().getStringArray(R.array.fetch_frequency_keys);
  98.                 String[] d = getResources().getStringArray(R.array.fetch_frequency_display);
  99.                 String displayFrequency = d[0];
  100.                 String frequency = mFetchFrequencyPreference.getValue();
  101.                 for (int i = 0; i < k.length; i++) {
  102.                         if (frequency.equals(k[i])) {
  103.                                 displayFrequency = d[i];
  104.                                 break;
  105.                         }
  106.                 }
  107.                 MessageFormat sf = new MessageFormat(getText(R.string.summary_preference_frequency).toString());
  108.                 mFetchFrequencyPreference.setSummary(sf.format(new Object[] { displayFrequency }));
  109.         }

  110.         protected void updateRingtone(Object newValue) {
  111.                 String ringtone = (String) newValue;
  112.                 Uri uri;
  113.                 Ringtone rt;
  114.                 if (ringtone == null) {
  115.                         uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  116.                 } else if ("".equals(ringtone)) {
  117.                         mNotificationRingtone.setSummary(R.string.summary_preference_no_ringtone);
  118.                 } else {
  119.                         uri = Uri.parse(ringtone);
  120.                         rt = RingtoneManager.getRingtone(this, uri);
  121.                         mNotificationRingtone.setSummary(rt.getTitle(this));
  122.                 }
  123.         }

  124.         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  125.                 if (key.equals(KEY_FETCH_FREQUENCY)) {
  126.                         updateFrequency();
  127.                 }
  128.                 if (key.equals(KEY_TWITTER_USERNAME) || key.equals(KEY_TWITTER_PASSWORD)) {
  129.                         if (mEditTextUsername.getText() != null && mEditTextUsername.getText().length() > 0 && mEditTextPassword.getText() != null && mEditTextPassword.getText().length() > 0) {
  130.                                 showDialog(DIALOG_CHECKING_CREDENTIALS);
  131.                                 Thread thread = new Thread(mCheckCredentials);
  132.                                 thread.start();
  133.                         }
  134.                 }
  135.         };

  136.         public boolean onPreferenceChange(Preference preference, Object newValue) {
  137.                 Log.i(TAG, preference.getKey());
  138.                 if (preference.getKey().equals(KEY_RINGTONE_PREFERENCE)) {
  139.                         updateRingtone(newValue);
  140.                         return true;
  141.                 }
  142.                 return false;
  143.         }

  144.         @Override
  145.         protected Dialog onCreateDialog(int id) {
  146.                 switch (id) {
  147.                 case DIALOG_AUTHENTICATION_FAILED:
  148.                         return new AlertDialog.Builder(this)
  149.                                 .setIcon(android.R.drawable.ic_dialog_alert)
  150.                                 .setTitle(R.string.dialog_title_authentication_failed)
  151.                                 .setMessage(R.string.dialog_summary_authentication_failed)
  152.                                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  153.                                         public void onClick(DialogInterface Dialog, int whichButton) {
  154.                                         }
  155.                                 }).create();

  156.                 case DIALOG_SERVICE_UNAVAILABLE:
  157.                         return new AlertDialog.Builder(this)
  158.                                 .setIcon(android.R.drawable.ic_dialog_alert)
  159.                                 .setTitle(R.string.dialog_title_service_unavailable)
  160.                                 .setMessage(R.string.dialog_summary_service_unavailable)
  161.                                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  162.                                         public void onClick(DialogInterface Dialog, int whichButton) {
  163.                                         }
  164.                                 }).create();

  165.                 case DIALOG_CHECKING_CREDENTIALS:
  166.                         mProgressDialog = new ProgressDialog(this);
  167.                         mProgressDialog.setTitle(R.string.dialog_title_checking_credentials);
  168.                         mProgressDialog.setMessage(getText(R.string.dialog_summary_checking_credentials));
  169.                         return mProgressDialog;

  170.                 case DIALOG_CONNECTION_TIMEOUT:
  171.                         return new AlertDialog.Builder(this)
  172.                                 .setIcon(android.R.drawable.ic_dialog_alert)
  173.                                 .setTitle(R.string.dialog_title_connection_timeout)
  174.                                 .setMessage(R.string.dialog_summary_connection_timeout)
  175.                                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  176.                                         public void onClick(DialogInterface Dialog, int whichButton) {
  177.                                         }
  178.                                 }).create();

  179.                 default:
  180.                         return super.onCreateDialog(id);
  181.                 }
  182.         }

  183.         private Handler mHandler = new Handler() {
  184.                 @Override
  185.                 public void handleMessage(Message msg) {
  186.                         switch (msg.what) {
  187.                         case MSG_ACCOUNT_VALID:
  188.                                 mAuthentiated = true;
  189.                                 dismissDialog(DIALOG_CHECKING_CREDENTIALS);
  190.                                 mAutomaticUpdates.setEnabled(true);
  191.                                 Toast.makeText(PreferencesActivity.this, R.string.authentication_successful, Toast.LENGTH_SHORT).show();
  192.                                 break;
  193.                         case MSG_ACCOUNT_INVALID:
  194.                                 mAuthentiated = false;
  195.                                 mAutomaticUpdates.setEnabled(false);
  196.                                 dismissDialog(DIALOG_CHECKING_CREDENTIALS);
  197.                                 showDialog(DIALOG_AUTHENTICATION_FAILED);
  198.                                 break;
  199.                         case MSG_SERVICE_UNAVAILABLE_ERROR:
  200.                                 mAuthentiated = false;
  201.                                 dismissDialog(DIALOG_CHECKING_CREDENTIALS);
  202.                                 showDialog(DIALOG_SERVICE_UNAVAILABLE);
  203.                                 break;
  204.                         case MSG_CONNECTION_EXCEPTION:
  205.                                 mAuthentiated = false;
  206.                                 dismissDialog(DIALOG_CHECKING_CREDENTIALS);
  207.                                 int mId = 0;
  208.                                 try {
  209.                                         mId = Integer.parseInt((String) msg.obj);
  210.                                 } catch (Exception e) {
  211.                                 }
  212.                                 switch (mId) {
  213.                                 case 404:
  214.                                         mId = R.string.error_twitter_404;
  215.                                         break;
  216.                                 default:
  217.                                         mId = R.string.error_connection_error;
  218.                                         break;
  219.                                 }
  220.                                 Toast.makeText(PreferencesActivity.this, mId, Toast.LENGTH_LONG).show();
  221.                                 break;
  222.                         case MSG_SOCKET_TIMEOUT_EXCEPTION:
  223.                                 mAuthentiated = false;
  224.                                 dismissDialog(DIALOG_CHECKING_CREDENTIALS);
  225.                                 showDialog(DIALOG_CONNECTION_TIMEOUT);
  226.                                 break;
  227.                         }
  228.                 }
  229.         };

  230.         private Runnable mCheckCredentials = new Runnable() {
  231.                 public void run() {
  232.                         Connection c = new Connection(mEditTextUsername.getText(), mEditTextPassword.getText());
  233.                         try {
  234.                                 JSONObject jo = c.verifyCredentials();
  235.                                 if (jo.optInt("id") > 0) {
  236.                                         mHandler.sendMessage(mHandler.obtainMessage(MSG_ACCOUNT_VALID, 1, 0));
  237.                                         return;
  238.                                 }
  239.                         } catch (JSONException e) {
  240.                                 Log.e(TAG, e.getMessage());
  241.                         } catch (ConnectionException e) {
  242.                                 mHandler.sendMessage(mHandler.obtainMessage(MSG_CONNECTION_EXCEPTION, 1, 0, e.getMessage()));
  243.                                 return;
  244.                         } catch (ConnectionAuthenticationException e) {
  245.                                 mHandler.sendMessage(mHandler.obtainMessage(MSG_ACCOUNT_INVALID, 1, 0));
  246.                                 return;
  247.                         } catch (ConnectionUnavailableException e) {
  248.                                 mHandler.sendMessage(mHandler.obtainMessage(MSG_SERVICE_UNAVAILABLE_ERROR, 1, 0));
  249.                                 return;
  250.                         } catch (SocketTimeoutException e) {
  251.                                 mHandler.sendMessage(mHandler.obtainMessage(MSG_SOCKET_TIMEOUT_EXCEPTION, 1, 0));
  252.                                 return;
  253.                         }
  254.                         mHandler.sendMessage(mHandler.obtainMessage(MSG_ACCOUNT_INVALID, 1, 0));
  255.                 }
  256.         };
  257. }
复制代码


附件: 您需要登录才可以下载或查看附件。没有帐号?免费加入
1

评分人数

生有涯而知无涯

不错.先收藏了.

TOP

回复 1# pengsky2002

看看先。

TOP

好东西,顶你!
兴趣是最好的老师

TOP

谢谢~看看  怎么现在才扣1分了呢~~
浑浑噩噩过一辈,不如潇洒走一回

TOP

见好就收

TOP

回复 6# momoch1314


    兄弟你说twitter是干嘛的

TOP

好东西啊

TOP

好东西啊

TOP

謝謝分享~
正好要研究它

TOP

非常感谢!!!

TOP

谢谢

TOP

捧个人场

TOP

不错,谢谢

TOP

额,原以为会有点讲解

TOP

返回列表