| | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.core.content.ContextCompat; |
| | | import androidx.fragment.app.Fragment; |
| | | import androidx.lifecycle.Observer; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.adapter.TabAdapter; |
| | | import com.dayu.pipirrapp.bean.db.TagBean; |
| | | import com.dayu.pipirrapp.dao.DaoSingleton; |
| | | import com.dayu.pipirrapp.databinding.ActivityMainBinding; |
| | | import com.dayu.pipirrapp.fragment.MainFragment; |
| | | import com.dayu.pipirrapp.fragment.MapFragment; |
| | | import com.dayu.pipirrapp.fragment.MyFragment; |
| | | import com.dayu.pipirrapp.fragment.OrderFragment; |
| | | import com.dayu.pipirrapp.net.MqttManager; |
| | | import com.dayu.pipirrapp.tool.InspectionUtils; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.NetUtils; |
| | | import com.jeremyliao.liveeventbus.LiveEventBus; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | private ActivityMainBinding binding; |
| | | private List<Fragment> fragments = new ArrayList<>(); |
| | | private long mExitTime; |
| | | MqttManager mqttManager; |
| | | |
| | | private enum Tab { |
| | | ORDER, MAP, MY |
| | |
| | | setupFragments(); |
| | | initView(); |
| | | initTab(); |
| | | |
| | | try { |
| | | TagBean tagBean = DaoSingleton.getInstance(this).tagDao().findFirst(); |
| | | MyApplication.myApplication.myTag = tagBean.getTag(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | mqttManager = new MqttManager(this, this); |
| | | mqttManager.connect(); |
| | | LiveEventBus.get(CommonKeyName.NetworkCallback).observeForever(new Observer<Object>() { |
| | | @Override |
| | | public void onChanged(Object o) { |
| | | switch ((int) o) { |
| | | case NetUtils.Available: |
| | | MyLog.i("MqttManager>>>Lost"); |
| | | InspectionUtils.aginPutInspectionData(MainActivity.this); |
| | | break; |
| | | case NetUtils.Lost: |
| | | |
| | | break; |
| | | } |
| | | } |
| | | }); |
| | | registNetCallBack(); |
| | | } |
| | | |
| | | @Override |
| | | protected void onStart() { |
| | | super.onStart(); |
| | | |
| | | } |
| | | |
| | | private void setupFragments() { |
| | | fragments.add(new MainFragment()); |
| | | fragments.add(new OrderFragment()); |
| | | fragments.add(new MapFragment()); |
| | | fragments.add(new MyFragment()); |
| | | } |
| | |
| | | return true; |
| | | } |
| | | return super.onKeyDown(keyCode, event); |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | //关闭MQ |
| | | try { |
| | | mqttManager.disconnect(); |
| | | unregisterNetworkCallback(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | binding.myImg.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bottom_my_black)); |
| | | binding.myText.setTextColor(ContextCompat.getColor(this, R.color.black)); |
| | | } |
| | | |
| | | // 注册网络监控 |
| | | private void registNetCallBack() { |
| | | NetUtils.registerNetCallBack(this); |
| | | } |
| | | |
| | | private void unregisterNetworkCallback() { |
| | | NetUtils.unregisterReceiver(this); |
| | | } |
| | | |
| | | |
| | | } |