Java tutorial
/* * Copyright (c) 2013 The CCP project authors. All Rights Reserved. * * Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license * that can be found in the LICENSE file in the root of the web site. * * http://www.yuntongxun.com * * An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ package com.gao.im.ui; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import com.gao.im.R; import com.gao.im.common.utils.DensityUtil; import com.umeng.analytics.MobclickAgent; /** * ?View?View ? * @author ? * @date 2014-12-4 * @version 4.0 */ public abstract class CCPFragment extends Fragment { /**??????*/ private View mLayoutListenerView; /**?,??*/ private LinearLayout mCCProotView; /**?getLayoutId()?*/ private ViewGroup mContentView; /***/ private InternalReceiver internalReceiver; /**?????*/ private boolean isFinish = false;; final Handler mSupperHandler = new Handler() { @Override public void dispatchMessage(Message msg) { // TODO Auto-generated method stub super.dispatchMessage(msg); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (mLayoutListenerView == null) { mLayoutListenerView = inflater.inflate(R.layout.ccp_fragment, null); mCCProotView = (LinearLayout) mLayoutListenerView.findViewById(R.id.ccp_root_view); if (getTitleLayoutId() != -1) { // ???ActionBar?? mCCProotView.addView(inflater.inflate(getTitleLayoutId(), null), LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.getMetricsDensity(getActivity(), 50.0F)); } // ?? if (getLayoutId() != -1) { mContentView = (ViewGroup) inflater.inflate(getLayoutId(), null); mCCProotView.addView(mContentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); } } ViewGroup mViewRoot = (ViewGroup) mLayoutListenerView.getParent(); if (mViewRoot != null) { mViewRoot.removeView(mLayoutListenerView); } return mLayoutListenerView; } @Override public void onDetach() { super.onDetach(); try { // ? getActivity().unregisterReceiver(internalReceiver); } catch (Exception e) { } } /** * ? * @param keyCode * @param event * @return */ public boolean onKeyDown(int keyCode, KeyEvent event) { if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && event.getAction() == KeyEvent.ACTION_DOWN) { finish(); return true; } return false; } /** * Action???? * Action * @param actionArray */ protected final void registerReceiver(String[] actionArray) { if (actionArray == null) { return; } IntentFilter intentfilter = new IntentFilter(); for (String action : actionArray) { intentfilter.addAction(action); } //intentfilter.addAction(CASIntent.ACTION_SERVICE_DESTORY); //intentfilter.addAction(CASIntent.ACTION_FORCE_DEACTIVE); if (internalReceiver == null) { internalReceiver = new InternalReceiver(); } getActivity().registerReceiver(internalReceiver, intentfilter); } /** * Handler * @return */ public android.os.Handler getSupperHandler() { return mSupperHandler; } /** * ?? */ public void finish() { if (getActivity() == null) { return; } if (isFinish) { getActivity().finish(); return; } getActivity().getFragmentManager().popBackStack(); } @Override public void onResume() { super.onResume(); MobclickAgent.onPageStart("MainScreen"); //? } @Override public void onPause() { super.onPause(); MobclickAgent.onPageEnd("MainScreen"); } /** * View * @param paramInt * @return */ public final View findViewById(int paramInt) { return getView().findViewById(paramInt); } /** * ???? * ? * @param context * @param intent */ protected void handleReceiver(Context context, Intent intent) { // ? } /** * ?????ID * @return ??ID */ protected abstract int getLayoutId(); /** * ???? * @return */ public int getTitleLayoutId() { return -1; } /** * ?? * @author ? * @date 2014-12-4 * @version 4.0 */ private class InternalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent == null || intent.getAction() == null) { return; } handleReceiver(context, intent); } } }