子线程问题
This commit is contained in:
parent
925a1de1ca
commit
f8126d6042
|
|
@ -1,15 +1,12 @@
|
|||
package com.bonus.canteen.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.camera2.CameraAccessException;
|
||||
import android.hardware.camera2.CameraManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.DhcpInfo;
|
||||
import android.net.DnsResolver;
|
||||
import android.net.Network;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.net.wifi.WifiInfo;
|
||||
|
|
@ -30,16 +27,12 @@ import android.widget.ListView;
|
|||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bonus.canteen.R;
|
||||
import com.bonus.canteen.activity.order.MachineSaleActivity;
|
||||
import com.bonus.canteen.adapter.initialization.IniListAdapter;
|
||||
import com.bonus.canteen.adapter.initialization.entity.ListBean;
|
||||
import com.bonus.canteen.adapter.menu.utils.OkHttpService;
|
||||
import com.bonus.canteen.adapter.menu.utils.StringHelper;
|
||||
import com.bonus.canteen.adapter.menu.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.adapter.menu.utils.WorkConfig;
|
||||
import com.bonus.canteen.adapter.order.entity.OrderListAndroidVO;
|
||||
import com.bonus.canteen.adapter.utils.OkHttpService;
|
||||
import com.bonus.canteen.adapter.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.adapter.utils.WorkConfig;
|
||||
import com.bonus.canteen.core.BaseActivity;
|
||||
import com.bonus.canteen.databinding.ActivityCanteenOperationBinding;
|
||||
import com.bonus.canteen.databinding.InitializationMainBinding;
|
||||
import com.bonus.canteen.db.AppDatabase;
|
||||
import com.bonus.canteen.db.entity.user.CustInfo;
|
||||
|
|
@ -49,12 +42,10 @@ import com.google.gson.reflect.TypeToken;
|
|||
import com.hjq.permissions.OnPermissionCallback;
|
||||
import com.hjq.permissions.Permission;
|
||||
import com.hjq.permissions.XXPermissions;
|
||||
import com.umeng.commonsdk.debug.E;
|
||||
import com.xuexiang.xui.XUI;
|
||||
import com.xuexiang.xui.utils.XToastUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MalformedURLException;
|
||||
|
|
@ -66,16 +57,7 @@ import java.util.Collections;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.security.auth.callback.Callback;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.Unbinder;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
|
|
@ -83,27 +65,15 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
|
||||
private IniListAdapter iniListAdapter;
|
||||
|
||||
private List<ListBean> initializeList = new ArrayList<>();
|
||||
|
||||
private List<ListBean> listBean = new ArrayList<>();
|
||||
private ListView listView;
|
||||
|
||||
private Map<Integer, List<ListBean>> map = new LinkedHashMap<>();
|
||||
|
||||
public OkHttpService service = new OkHttpService();
|
||||
|
||||
private static Integer UPDATE_ADAPTER = 1;
|
||||
|
||||
|
||||
|
||||
private Handler handler = new Handler(Looper.getMainLooper()){
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == UPDATE_ADAPTER){
|
||||
List<ListBean> data = (List<ListBean>) msg.obj;
|
||||
iniListAdapter.addItem(data);
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
private static final Integer UPDATE_ADAPTER = 1;
|
||||
|
||||
@Override
|
||||
protected InitializationMainBinding viewBindingInflate(LayoutInflater inflater) {
|
||||
|
|
@ -123,7 +93,7 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
//检查网络是否可用
|
||||
getNetWorkStatus();
|
||||
//检查摄像头是否可用
|
||||
isCameraAvailabl();
|
||||
checkIfTheCameraIsAvailable();
|
||||
// //获取人员信息
|
||||
// iniListAdapter.addItem(new ListBean("开始更新人员信息...",0));
|
||||
// getPersonMessage();
|
||||
|
|
@ -165,13 +135,7 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
getPersonMessage();
|
||||
|
||||
} else {
|
||||
runOnUiThread(() -> {
|
||||
listBean.add(new ListBean("人员更新完成", 0));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
});
|
||||
|
||||
sendMessage("人员更新完成", 0);
|
||||
}
|
||||
Log.i("getPersonMessage custInfoList", custInfoList.size() + "");
|
||||
}
|
||||
|
|
@ -181,35 +145,25 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
|
||||
|
||||
private void initViews() {
|
||||
Log.i("initDate listBean", listBean.size() + "");
|
||||
iniListAdapter = new IniListAdapter(this, listBean);
|
||||
binding.iniList.setAdapter(iniListAdapter);
|
||||
listView = findViewById(R.id.ini_list);
|
||||
iniListAdapter = new IniListAdapter(this, initializeList);
|
||||
listView.setAdapter(iniListAdapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查摄像头是否可用
|
||||
*/
|
||||
public void isCameraAvailabl() {
|
||||
public void checkIfTheCameraIsAvailable() {
|
||||
CameraManager cameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
|
||||
try {
|
||||
String[] cameraIds = cameraManager.getCameraIdList();
|
||||
if (cameraIds.length > 0) {
|
||||
listBean.add(new ListBean("摄像头检测正常", 0));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
sendMessage("摄像头检测正常", 0);
|
||||
} else {
|
||||
listBean.add(new ListBean("摄像头检测异常", 1));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
sendMessage("摄像头检测异常", 1);
|
||||
}
|
||||
Log.e("xxxxx=====",listBean.toString());
|
||||
} catch (CameraAccessException e) {
|
||||
listBean.add(new ListBean("摄像头检测异常", 1));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
sendMessage("摄像头检测异常", 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -218,71 +172,41 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
*/
|
||||
public void getNetWorkStatus() {
|
||||
try {
|
||||
ThreadPoolManager.getExecutor().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
{
|
||||
// 在后台线程中执行网络操作
|
||||
URL url = null; // 使用 Google 的 URL
|
||||
try {
|
||||
url = new URL("https://www.baidu.com");
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
connection.setRequestMethod("GET");
|
||||
} catch (ProtocolException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
connection.setConnectTimeout(5000); // 设置超时时间
|
||||
try {
|
||||
connection.connect();
|
||||
int responseCode = connection.getResponseCode();
|
||||
Log.i("responseCode", "" + responseCode);
|
||||
if (responseCode == 200) {
|
||||
listBean.add(new ListBean("设备网络连接正常", 0));
|
||||
//iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
} else {
|
||||
listBean.add(new ListBean("设备网络连接异常", 1));
|
||||
//iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 在主线程中调用 addItem
|
||||
Log.e("xxxxx", listBean.toString());
|
||||
iniListAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
// runOnUiThread(() -> {
|
||||
// iniListAdapter.notifyDataSetChanged();
|
||||
//
|
||||
// });
|
||||
|
||||
ThreadPoolManager.getExecutor().execute(() -> {
|
||||
// 在后台线程中执行网络操作
|
||||
URL url = null; // 使用 Google 的 URL
|
||||
try {
|
||||
url = new URL("https://www.baidu.com");
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
connection.setRequestMethod("GET");
|
||||
} catch (ProtocolException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
connection.setConnectTimeout(5000); // 设置超时时间
|
||||
try {
|
||||
connection.connect();
|
||||
int responseCode = connection.getResponseCode();
|
||||
Log.i("responseCode", "" + responseCode);
|
||||
if (responseCode == 200) {
|
||||
sendMessage("设备网络连接正常", 0);
|
||||
} else {
|
||||
sendMessage("设备网络连接异常", 1);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
Log.e("xxxxx-----", listBean.toString());
|
||||
} catch (Exception e) {
|
||||
listBean.add(new ListBean("设备网络连接异常", 1));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
sendMessage("设备网络连接异常", 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -314,17 +238,12 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
|
||||
Log.e("getMemoryMessage totalSpaceMB", "" + totalSpaceGB);
|
||||
Log.e("getMemoryMessage availableSpaceMB", "" + availableSpaceGB);
|
||||
listBean.add(new ListBean("设备磁盘空间正常,可用空间" + availableSpaceGB, 0));
|
||||
// iniListAdapter.addItem(listBean);
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, listBean);
|
||||
handler.sendMessage(msg);
|
||||
sendMessage("设备磁盘空间正常,可用空间" + availableSpaceGB, 0);
|
||||
} catch (Exception e) {
|
||||
|
||||
sendMessage("获取设备磁盘空间异常", 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void initRightDate() {
|
||||
String netWorkType = getNetworkType(this);
|
||||
String imei = getImei();
|
||||
|
|
@ -400,6 +319,7 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
}
|
||||
|
||||
//获取子网掩码
|
||||
@SuppressLint("DefaultLocale")
|
||||
private static String formatIpAddress(int ipAddress) {
|
||||
return String.format("%d.%d.%d.%d",
|
||||
(ipAddress & 0xff),
|
||||
|
|
@ -408,6 +328,7 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
(ipAddress >> 24 & 0xff));
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
public static String getSubnetMask(Context context) {
|
||||
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
||||
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||||
|
|
@ -559,4 +480,21 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
|
||||
}
|
||||
|
||||
private void sendMessage(String listName, int status) {
|
||||
initializeList.add(new ListBean(listName, status));
|
||||
Message msg = handler.obtainMessage(UPDATE_ADAPTER, initializeList);
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
|
||||
private Handler handler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == UPDATE_ADAPTER) {
|
||||
List<ListBean> data = (List<ListBean>) msg.obj;
|
||||
Log.e("sendMessage--handler", data.toString());
|
||||
iniListAdapter.setData(data);
|
||||
iniListAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,7 @@ import android.widget.BaseAdapter;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.bonus.canteen.R;
|
||||
import com.bonus.canteen.activity.InitializationActivity;
|
||||
import com.bonus.canteen.activity.order.MachineSaleActivity;
|
||||
import com.bonus.canteen.adapter.initialization.entity.ListBean;
|
||||
import com.bonus.canteen.adapter.menu.entity.DishEntity;
|
||||
import com.bonus.canteen.adapter.menu.entity.SalesMenuEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -50,25 +46,32 @@ public class IniListAdapter extends BaseAdapter {
|
|||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder holder = null;
|
||||
if (convertView == null) {
|
||||
LayoutInflater inflater = LayoutInflater.from(context);
|
||||
convertView = inflater.inflate(R.layout.initialization_left_item, parent, false);
|
||||
TextView titleText = convertView.findViewById(R.id.ini_list_text);
|
||||
ListBean bean = list.get(position);
|
||||
Log.e("ssss", String.valueOf(position));
|
||||
Log.e("ssss",bean.toString());
|
||||
int status = bean.getStatus();
|
||||
titleText.setText(bean.getListName());
|
||||
if(status == 1){
|
||||
titleText.setTextColor(Color.RED);
|
||||
}
|
||||
|
||||
holder = new ViewHolder();
|
||||
convertView = inflater.inflate(R.layout.initialization_left_item, null);
|
||||
holder.titleText = convertView.findViewById(R.id.ini_list_text);
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
ListBean bean = list.get(position);
|
||||
Log.e("ssss", String.valueOf(position));
|
||||
Log.e("ssss",bean.toString());
|
||||
int status = bean.getStatus();
|
||||
holder.titleText.setText(bean.getListName());
|
||||
if(status == 1){
|
||||
holder.titleText.setTextColor(Color.RED);
|
||||
}
|
||||
return convertView;
|
||||
}
|
||||
|
||||
public void addItem(List<ListBean> item) {
|
||||
public void setData(List<ListBean> item) {
|
||||
list = new ArrayList<>(item);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
TextView titleText;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue