首先感謝開源中國(guó)為我們帶來(lái)了學(xué)習(xí)資源:http://www.oschina.net/p/oschina-android-app
這份代碼雖然存在不少小瑕疵耸序,但總體上是一個(gè)功能齊備的應(yīng)用镀赌,而且關(guān)鍵是其代碼非常易讀
今天先分析一下啟動(dòng)及主界面
BaseApplication extends Application
這個(gè)類中主要是做了一些功能封裝:
- 已讀文章列表相關(guān)
/**
* 放入已讀文章列表中
*
*/
public static void putReadedPostList(String prefFileName, String key,
String value) {
SharedPreferences preferences = getPreferences(prefFileName);
int size = preferences.getAll().size();
Editor editor = preferences.edit();
if (size >= 100) {
editor.clear();
}
editor.putString(key, value);
apply(editor);
}
- SharedPreferences相關(guān)
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void apply(SharedPreferences.Editor editor) {
if (sIsAtLeastGB) {
editor.apply();
} else {
editor.commit();
}
}
public static void set(String key, int value) {
Editor editor = getPreferences().edit();
editor.putInt(key, value);
apply(editor);
}
- 獲取String相關(guān)
public static String string(int id) {
return _resource.getString(id);
}
public static String string(int id, Object... args) {
return _resource.getString(id, args);
}
- Toast相關(guān)
public static void showToast(String message, int icon) {
showToast(message, Toast.LENGTH_LONG, icon, Gravity.BOTTOM);
}
public static void showToastShort(int message) {
showToast(message, Toast.LENGTH_SHORT, 0);
}
AppContext extends BaseApplication
正式的自定義Application了耘子,所做的事很簡(jiǎn)單,看如下代碼:
@Override
public void onCreate() {
super.onCreate();
instance = this;
init();
initLogin();
UIHelper.sendBroadcastForNotice(this); // 注一焰宣,其后會(huì)對(duì)這里進(jìn)行說(shuō)明
}
private void init() {
// 初始化網(wǎng)絡(luò)請(qǐng)求
AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
ApiHttpClient.setHttpClient(client);
ApiHttpClient.setCookie(ApiHttpClient.getCookie(this));
// Log控制器
KJLoger.openDebutLog(true);
TLog.DEBUG = BuildConfig.DEBUG;
// Bitmap緩存地址
HttpConfig.CACHEPATH = "OSChina/imagecache";
}
登錄信息相關(guān):
private void initLogin() {
User user = getLoginUser();
if (null != user && user.getId() > 0) {
login = true;
loginUid = user.getId();
} else {
this.cleanLoginInfo();
}
}
還有就是代碼中封裝了Properties的使用,雖然使用SharedPreferences會(huì)占用內(nèi)存,但個(gè)人認(rèn)為這里數(shù)據(jù)量很少并不是很有必要挂绰,實(shí)際操作代碼封裝在AppConfig中:
public Properties getProperties() {
return AppConfig.getAppConfig(this).get();
}
public void setProperty(String key, String value) {
AppConfig.getAppConfig(this).set(key, value);
}
AppStart extends Activity
入口Activity,實(shí)現(xiàn)啟動(dòng)頁(yè)服赎,方法是設(shè)置其style為全屏無(wú)ActionBar葵蒂,延時(shí)跳轉(zhuǎn):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 防止第三方跳轉(zhuǎn)時(shí)出現(xiàn)雙實(shí)例
Activity aty = AppManager.getActivity(MainActivity.class);
if (aty != null && !aty.isFinishing()) {
finish();
}
// SystemTool.gc(this); //針對(duì)性能好的手機(jī)使用,加快應(yīng)用相應(yīng)速度
final View view = View.inflate(this, R.layout.app_start, null);
setContentView(view);
// 漸變展示啟動(dòng)屏
AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
aa.setDuration(800);
view.startAnimation(aa);
aa.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
redirectTo();
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationStart(Animation animation) {}
});
}
另外說(shuō)一句重虑,該應(yīng)用使用了一個(gè)AppManager來(lái)對(duì)所有Activity進(jìn)行堆棧式管理践付,實(shí)現(xiàn)完全退出應(yīng)用。
而跳轉(zhuǎn)到主Activity之前缺厉,則先開啟了一個(gè)用來(lái)上傳Log的Service:
/**
* 跳轉(zhuǎn)到...
*/
private void redirectTo() {
Intent uploadLog = new Intent(this, LogUploadService.class);
startService(uploadLog);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
MainActivity extends ActionBarActivity
這里做的事就比較多了永高,只撿關(guān)鍵的講,上面的注一在initView()中有了眉目:
IntentFilter filter = new IntentFilter(Constants.INTENT_ACTION_NOTICE);
filter.addAction(Constants.INTENT_ACTION_LOGOUT);
registerReceiver(mReceiver, filter); // 注二
NoticeUtils.bindToService(this);
NoticeUtils中:
public static boolean bindToService(Context context) {
return bindToService(context, null);
}
public static boolean bindToService(Context context,
ServiceConnection callback) {
context.startService(new Intent(context, NoticeService.class));
ServiceBinder sb = new ServiceBinder(callback);
sConnectionMap.put(context, sb);
return context.bindService(
(new Intent()).setClass(context, NoticeService.class), sb, 0);
}
最終是綁定了一個(gè)NoticeService提针,而在注一中命爬,其執(zhí)行代碼正為發(fā)送目標(biāo)為NoticeService的廣播:
/**
* 發(fā)送通知廣播
*
* @param context
*/
public static void sendBroadcastForNotice(Context context) {
Intent intent = new Intent(NoticeService.INTENT_ACTION_BROADCAST);
context.sendBroadcast(intent);
}
而在NoticeService之中,其后續(xù)動(dòng)作為發(fā)送Action為Constants.INTENT_ACTION_NOTICE的廣播辐脖,該廣播許多地方都會(huì)監(jiān)聽饲宛,而在MainActivity中也有注冊(cè)監(jiān)聽(見注二),最后會(huì)有一個(gè)BadgeView 控件發(fā)生改變:
private BadgeView mBvNotice;
public static Notice mNotice;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.INTENT_ACTION_NOTICE)) {
mNotice = (Notice) intent.getSerializableExtra("notice_bean");
int atmeCount = mNotice.getAtmeCount();// @我
int msgCount = mNotice.getMsgCount();// 留言
int reviewCount = mNotice.getReviewCount();// 評(píng)論
int newFansCount = mNotice.getNewFansCount();// 新粉絲
int newLikeCount = mNotice.getNewLikeCount();// 收到贊
int activeCount = atmeCount + reviewCount + msgCount
+ newFansCount + newLikeCount;
Fragment fragment = getCurrentFragment();
if (fragment instanceof MyInformationFragment) {
((MyInformationFragment) fragment).setNotice();
} else {
if (activeCount > 0) {
mBvNotice.setText(activeCount + "");
mBvNotice.show();
} else {
mBvNotice.hide();
mNotice = null;
}
}
} else if (intent.getAction()
.equals(Constants.INTENT_ACTION_LOGOUT)) {
mBvNotice.hide();
mNotice = null;
}
}
};
主界面實(shí)現(xiàn)
FragmentTabHost即可,布局片段如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/windows_bg" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip" >
<net.oschina.app.widget.MyFragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="?attr/lineColor" />
</RelativeLayout>
<!-- 快速操作按鈕 -->
<ImageView
android:id="@+id/quick_option_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@null"
android:src="@drawable/btn_quickoption_selector" />
</FrameLayout>
</LinearLayout>
realtabcontent分割剩余空間嗜价,為實(shí)際內(nèi)容頁(yè):
在initView()中進(jìn)行了setup:
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
中鍵點(diǎn)擊的效果不一樣艇抠,不是Fragment頁(yè)面,而是一個(gè)自定義Dialog了:
所以炭剪,實(shí)現(xiàn)上quick_option_iv這個(gè)ImageView在布局中對(duì)第三個(gè)Tab進(jìn)行了遮擋练链。
而第三個(gè)Tab也需要特殊處理:它在點(diǎn)擊時(shí)不切換頁(yè)面。
在MyFragmentTabHost extends FragmentTabHost中:
@Override
public void onTabChanged(String tag) {
if (tag.equals(mNoTabChangedTag)) {// 對(duì)第三個(gè)Tab特殊處理
setCurrentTabByTag(mCurrentTag);
} else {
super.onTabChanged(tag);
mCurrentTag = tag;
}
}