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();
}
}
BaseActivity
最新推荐文章于 2020-11-15 15:18:49 发布