Android Open Source - CommonLibs Base Activity






From Project

Back to project page CommonLibs.

License

The source code is released under:

Apache License

If you think the Android project CommonLibs 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

/**
 * <p>Title: BaseActivity.java</p>
 * <p>Description: </p>//www. j  a  v  a 2s.co  m
 * <p>Copyright: Copyright (c) 2013</p>
 * <p>Company: </p>
 * @author caisenchuan
 * @date 2013-9-8
 * @version 1.0
 */
package com.alex.common.activities;

import com.alex.common.AppControl;
import com.alex.common.R;
import com.alex.common.utils.DialogUtils;
import com.alex.common.utils.ToastUtils;
import com.alex.common.utils.KLog;
import com.alex.common.utils.ThreadUtils;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;

/**
 * ????Activity????????
 * @author caisenchuan
 *
 */
public abstract class BaseActivity extends Activity {
    /*--------------------------
     * ?????
     *-------------------------*/
    
    /*--------------------------
     * ???
     *-------------------------*/
    private static final String TAG_BASE = "BaseActivity";
    ///////////////////mHandler msg.what//////////////
    /**??Toast????obj?*/
    protected static final int MSG_SHOW_TOAST = 100;
    /**????????what?????????*/
    protected static final int MSG_EXTEND_BASE = 200;
    
    /*--------------------------
     * ????????
     *-------------------------*/
    /**????????*/
    protected AppControl mApp = null;
    /**Handler??*/
    protected Handler mBaseHandler = null;
    /**ActionBar??*/
    protected ActionBar mActionBar = null;
    /**???????*/
    private Dialog mProgressDialog = null;
    
    /*--------------------------
     * public??
     *-------------------------*/
    /**
     * ?????????????????????????????????????????????
     * ???????activity????????????????
     * ????onConfigurationChanged????????????AnroidMainifest.xml???
     * android:configChanges="xxxx"????????????
     * ????????????????onDestroy()???Activity??????????????Activity???
     * 
     * @param newConfig ???????
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // ????????????????????
        if (this.getResources().getConfiguration().orientation 
                == Configuration.ORIENTATION_LANDSCAPE) {
            //???????? ????????????????
            KLog.d(TAG_BASE, "landscapte");
        }
        else if (this.getResources().getConfiguration().orientation 
                == Configuration.ORIENTATION_PORTRAIT) {
            //???????? ????????????????
        }
        //???????????????????    
        if (newConfig.hardKeyboardHidden 
                == Configuration.HARDKEYBOARDHIDDEN_NO){ 
            //????????????????????????????
        } 
        else if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_YES){ 
            //??????????????????????????????
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        KLog.d(TAG_BASE, "onOptionsItemSelected , %s , %s", 
                          item.getItemId(), item.getTitle());

        boolean ret = false;
        switch(item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            
            default:
                ret = super.onOptionsItemSelected(item);
                break;
        }
        
        return ret;
    }
    
    /**
     * ??????Toast
     * @param id
     * @author caisenchuan
     */
    public void showToastOnUIThread(int id) {
        String str = getApplicationContext().getString(id);
        showToastOnUIThread(str);
    }
    
    /**
     * ??????Toast
     * @author caisenchuan
     * @param text
     */
    public void showToastOnUIThread(String text) {
        if(mBaseHandler != null) {
            Message msg = new Message();
            msg.what = MSG_SHOW_TOAST;
            msg.obj = text;
            mBaseHandler.sendMessage(msg);
        }
    }
    
    /*--------------------------
     * protected??packet??
     *-------------------------*/
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApp = (AppControl)getApplication();
        
        mActionBar = getActionBar();
        
        mProgressDialog = DialogUtils.buildSpinnerProgressDialog(this, getString(R.string.hint_progress));
        
        //????ActionBar????
        if(mActionBar != null) {
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        
        //????Handler
        mBaseHandler = new Handler() {
            public void handleMessage(Message msg) {
                if(msg.what == MSG_SHOW_TOAST) {
                    //??????Toast
                    String text = (String)msg.obj;
                    ToastUtils.showLongToast(BaseActivity.this, text, true);
                } else if(msg.what > MSG_EXTEND_BASE) {
                    //??????????
                    handleBaseMessage(msg);
                } else {
                    //???????????
                    super.handleMessage(msg);
                }
            };
        };
    }
        
    /**
     * ??Acitvity?UIHandler???????
     * @author caisenchuan
     * @param type
     */
    protected void sendMessageToBaseHandler(int what) {
        if(mBaseHandler != null) {
            Message msg = new Message();
            msg.what = what;
            mBaseHandler.sendMessage(msg);
        }
    }
    
    /**
     * ??Acitvity?UIHandler???????
     * @param what
     * @param arg1
     * @param arg2
     * @param obj
     * @author caisenchuan
     */
    protected void sendMessageToBaseHandler(int what, int arg1, int arg2, Object obj) {
        if(mBaseHandler != null) {
            Message msg = new Message();
            msg.what = what;
            msg.arg1 = arg1;
            msg.arg2 = arg2;
            msg.obj  = obj;
            mBaseHandler.sendMessage(msg);
        }
    }
    
    /**
     * ????BaseHandler?????
     * @param msg
     * @author caisenchuan
     */
    protected void handleBaseMessage(Message msg) {
        // TODO Auto-generated method stub
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * ??????????
     * @param str
     */
    protected void setProgressDialogTitle(String str) {
        mProgressDialog = DialogUtils.buildSpinnerProgressDialog(this, str);
    }
    
    /**
     * ????????
     */
    protected void showProgressDialog() {
        mProgressDialog.show();
    }
    
    /**
     * ??????????????
     */
    protected void dissmissProgressDialogOnUIThread() {
        ThreadUtils.postOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        });
    }
    /*--------------------------
     * private??
     *-------------------------*/

}




Java Source Code List

com.alex.common.AppConfig.java
com.alex.common.AppControl.java
com.alex.common.Err.java
com.alex.common.OnHttpRequestReturnListener.java
com.alex.common.activities.BaseActivity.java
com.alex.common.activities.ImageLoadActivity.java
com.alex.common.activities.WebViewActivity.java
com.alex.common.apis.HttpApi.java
com.alex.common.exception.RetErrorException.java
com.alex.common.utils.BackgroundHandler.java
com.alex.common.utils.BaiduMapUtils.java
com.alex.common.utils.DateTimeUtils.java
com.alex.common.utils.DeviceUtils.java
com.alex.common.utils.DialogUtils.java
com.alex.common.utils.FileUtils.java
com.alex.common.utils.ImageUtils.java
com.alex.common.utils.KLog.java
com.alex.common.utils.Misc.java
com.alex.common.utils.NetworkUtils.java
com.alex.common.utils.PrefUtils.java
com.alex.common.utils.ShareUtils.java
com.alex.common.utils.StringUtils.java
com.alex.common.utils.ThreadUtils.java
com.alex.common.utils.ToastUtils.java
com.alex.common.views.ZoomImageView.java