BaseActivity

public class BaseActivity extends AppCompatActivity {
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_DISCONN: //网络无连接
                    ll_loading.setVisibility(View.VISIBLE);
                    break;
                case NETWORK_CONN://网络连接
                    ll_loading.setVisibility(View.GONE);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    private   ViewGroup base_fl_body;
    protected TextView  tv_setting, tv_back, tv_verification;
    protected LinearLayout ll_loading;
    protected LinearLayout    ll_empty;

    @Override
    public void setContentView(View view) {
        View view1 = LayoutInflater.from(this).inflate(R.layout.activity_base, null);
        base_fl_body = (ViewGroup) view1.findViewById(R.id.base_fl_body);
        tv_setting = (TextView) view1.findViewById(R.id.tv_setting);
        tv_back = (TextView) view1.findViewById(R.id.tv_back);
        tv_verification = (TextView) view1.findViewById(R.id.tv_verification);
        ll_loading = (LinearLayout) view1.findViewById(R.id.ll_loading);
        ll_empty = (LinearLayout) view1.findViewById(R.id.ll_empty);
        ViewGroup viewGroup = new FrameLayout(this);
        //把页面插入在中间位置
        base_fl_body.addView(view);
        viewGroup.addView(view1);
        super.setContentView(viewGroup);
        bindNetWorkService(); //绑定网络状态服务
    }

    private ServiceConnection              mConn;
    private StatusBarSercive.updateNetWork mUpdateNetWork;
    public static final int NETWORK_CONN    = 300;//网络有连接
    public static final int NETWORK_DISCONN = 400;//网络无连接

    protected void bindNetWorkService() {
        mConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mUpdateNetWork = (StatusBarSercive.updateNetWork) service;
                mUpdateNetWork.setListener(new StatusBarSercive.StatusBarChangeListener() {
                    @Override
                    public void onNetworkChange(int networkType) {
                        switch (networkType) {
                            case NetWorkStatus.NO_NETWORK:
                                mHandler.sendEmptyMessage(NETWORK_DISCONN);
                                break;
                            default:
                                mHandler.sendEmptyMessage(NETWORK_CONN);
                                break;
                        }
                    }
                });
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };
        Intent intent = new Intent();
        intent.setAction("com.docom.changlongcall.service.networkSercive");
        intent.setPackage(getPackageName());
        bindService(intent, mConn, Context.BIND_AUTO_CREATE);
    }

    //通过类名启动Activity
    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    //返回首页
    public void goHome() {
        tv_back.setVisibility(View.GONE);
        tv_setting.setVisibility(View.VISIBLE);
        tv_verification.setVisibility(View.VISIBLE);
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        overridePendingTransition(0, 0);//去除转场动画
        finish();
    }

    //通过类名启动Activity,并且含有Bundle数据,定制转场动画
    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        startActivity(intent);
        overridePendingTransition(0, 0);
        finish();
    }

    /**
     * 退出应用
     */
    public void exitAPP() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    private Toast toast = null;

    public void showToast(String str) {
        if (TextUtils.isEmpty(str)) return;
        if (toast == null) {
            toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
        }
        toast.setText(str);
        toast.show();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值