|  |  |  | 
|---|
|  |  |  | package com.dayu.pipirrapp.activity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import android.net.ConnectivityManager; | 
|---|
|  |  |  | import android.net.Network; | 
|---|
|  |  |  | import android.os.Bundle; | 
|---|
|  |  |  | import android.view.KeyEvent; | 
|---|
|  |  |  | import android.view.LayoutInflater; | 
|---|
|  |  |  | import android.widget.Toast; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import androidx.annotation.NonNull; | 
|---|
|  |  |  | 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.bean.db.TagBean; | 
|---|
|  |  |  | import com.dayu.pipirrapp.dao.DaoSingleton; | 
|---|
|  |  |  | import com.dayu.pipirrapp.databinding.ActivityMainBinding; | 
|---|
|  |  |  | import com.dayu.pipirrapp.fragment.OrderFragment; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | super.onCreate(savedInstanceState); | 
|---|
|  |  |  | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)); | 
|---|
|  |  |  | setContentView(binding.getRoot()); | 
|---|
|  |  |  | registNetCallBack(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setupFragments(); | 
|---|
|  |  |  | initView(); | 
|---|
|  |  |  | initTab(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | mqttManager = new MqttManager(this); | 
|---|
|  |  |  | 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() { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    注册网络监控 | 
|---|
|  |  |  | private void registNetCallBack() { | 
|---|
|  |  |  | NetUtils.registerNetCallBack(this, networkCallback); | 
|---|
|  |  |  | NetUtils.registerNetCallBack(this); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void unregisterNetworkCallback() { | 
|---|
|  |  |  | NetUtils.unregisterReceiver(this, networkCallback); | 
|---|
|  |  |  | NetUtils.unregisterReceiver(this); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //网络监控 | 
|---|
|  |  |  | ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onAvailable(@NonNull Network network) { | 
|---|
|  |  |  | super.onAvailable(network); | 
|---|
|  |  |  | // 这里可以执行网络可用后的逻辑,比如进行数据请求等 | 
|---|
|  |  |  | MyLog.d("onAvailable"); | 
|---|
|  |  |  | InspectionUtils.aginPutInspectionData(MainActivity.this); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onLost(@NonNull Network network) { | 
|---|
|  |  |  | super.onLost(network); | 
|---|
|  |  |  | // 这里可以执行网络丢失后的逻辑,比如停止数据请求等 | 
|---|
|  |  |  | MyLog.d("onLost"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|