Activity基类

1.基类定义

View Code
/**
 * activity基类 提供退出方法,页面标题
 * 
 * @author 
 */
public class BaseActivity extends Activity {

    public Button bt_exit;
    public TextView tv_title;
    protected AlertDialog exitDialog;// 退出提示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        CallbinApplication.addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题栏
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        CallbinApplication.deleteActivity(this);
        super.onDestroy();
    }

    /**
     * 通过文字设置标题
     * 
     * @param activity
     * @param title
     */
    public void setTitle(Activity activity, String title) {
        tv_title = (TextView) activity.findViewById(R.id.tv_meeting_title);
        tv_title.setText(title);
    }

    /**
     * 通过id设置标题
     * 
     * @param activity
     * @param id
     */
    public void setTitle(Activity activity, int id) {
        tv_title = (TextView) activity.findViewById(R.id.tv_meeting_title);
        tv_title.setText(id);
    }

    /**
     * 退出应用程序方法
     * 
     * @param activity
     */
    public void exitAppcation(final Activity activity) {
        bt_exit = (Button) activity.findViewById(R.id.bt_exit);
        // 退出程序
        bt_exit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                exitAlert(activity);
            }
        });
    }

    /**
     * 弹出退出应用对话框
     * 
     * @param activity
     */
    public void exitAlert(Activity activity) {
        AlertDialog.Builder buider = new AlertDialog.Builder(activity);
        buider.setTitle(R.string.exit_alert_title);
        buider.setMessage(R.string.exit_alert_message);
        // 点击否是什么都不做只是把对话框去掉
        buider.setNegativeButton(R.string.no_string,
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        exitDialog.dismiss();
                    }
                });
        // 点击是时去掉对话框并做退出处理
        buider.setPositiveButton(R.string.yes_string,
                new android.content.DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        exitDialog.dismiss();
                        CallbinApplication.instance.onTerminate();
                    }

                });
        exitDialog = buider.create();
        exitDialog.show();
    }
    
    /**
     * Http请求返回值与处理是否是服务器正常相应回来的值
     * @param result 返回值的String字符串
     * @return result的返回值不是Http抛异常返回的 即result不等于null,“” “timeOut” ,"error"返回ture
     */
    protected boolean CheckHttpResponseJson(Context context,String result){
        if(result==null){
            Toast.makeText(context,
                    R.string.response_fail,
                    Toast.LENGTH_SHORT).show();
            return false;
        }else if(Constant.HTTPPARAMSNULL.equals(result)){
            Toast.makeText(context, R.string.url_null_error, 0).show();
        }else if(Constant.HTTPTIMEOUT.equals(result)){
            Toast.makeText(context, R.string.connect_timeout_exception,
                    Toast.LENGTH_SHORT).show();
            return false;
        }else if(Constant.HTTPERROR.equals(result)){
             Toast.makeText(context, R.string.sys_net_exception,
                     Toast.LENGTH_SHORT).show();
            return false;
        }else if(Constant.HTTP404.equals(result)){
             Toast.makeText(context, R.string.http_404,
                     Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}


 

原文地址:https://www.cnblogs.com/callbin/p/2854635.html