增加 - 服务查询信息通知,切换工位功能改造

master
wangh 7 months ago
parent d98500ae69
commit 91f8d10c60

@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:name=".base.MyApplication"
android:allowBackup="true"
@ -18,13 +20,17 @@
android:theme="@style/Theme.Aucmames"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<service
android:name=".service.MyService"
android:enabled="true"
android:exported="true"/>
<activity
android:name=".UnbindActivity"
android:exported="false" />
<activity
android:name=".ChangeActivity"
android:exported="false"
android:label="工位切换" />
android:exported="false" />
<activity
android:name=".base.BaseActivity"
android:exported="false" />

@ -7,6 +7,7 @@ import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aucma_mes.base.BaseActivity;
@ -21,20 +22,25 @@ import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ChangeActivity extends BaseActivity {
private List<DefectBeen> list;
public List<String> tagList;
private TextView barCodeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityChangeBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_change);
tagList = new ArrayList<>();
binding.setVm(this);
OkGo.<Result>get(url + "/base/productLine/findProductLineList?productLineType=2&stationType=2").tag(this)
.headers("Authorization", SharedPreferencesUtils.getstring("token", "")).execute(new MyRecultCall(dialog, this) {
getSupportActionBar().hide();
var tilte = getIntent().getStringExtra("title");
binding.setTitle(tilte);
barCodeView=binding.changeBoxCode;
OkGo.<Result>get(url + "/base/productLine/findProductLineList?productLineType=2&stationType=2").tag(this).headers("Authorization", SharedPreferencesUtils.getstring("token", "")).execute(new MyRecultCall(dialog, this) {
@Override
public void onSuccess(Response<Result> response) {
var body = response.body();
@ -52,19 +58,42 @@ public class ChangeActivity extends BaseActivity {
@Override
protected void sanInfo(String code) {
barCodeView.setText(code);
}
private String tag;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tag = tagList.get(position);
productLineCode = list.get(position).getProductLineCode();
Log.e("TAG", "选中工位:" + tag);
}
private String productLineCode;
public void changeSubmit(View view) {
SharedPreferencesUtils.putstring("stationName", tag);
SharedPreferencesUtils.putstring("stationCode", productLineCode);
finish();
}
public void changeOrderSubmit(View view) {
OkGo.<Result>post(url + "/api/switchOrders").tag(this)
.params("barCode", barCodeView.getText().toString().trim())
.params("stationCode", productLineCode)
.execute(new MyRecultCall(dialog, this) {
@Override
public void onSuccess(Response<Result> response) {
super.onSuccess(response);
Result result = response.body();
if (result.getCode()==200){
barCodeView.setText(null);
}
Toast.makeText(ChangeActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
}
});
}
}

@ -1,22 +1,20 @@
package com.example.aucma_mes;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.aucma_mes.databinding.ActivityHomePageBinding;
import com.example.aucma_mes.dialog.InfoDialog;
import com.example.aucma_mes.entity.LoginInfoBeen;
import com.example.aucma_mes.service.MyService;
import com.example.aucma_mes.utils.SharedPreferencesUtils;
import com.example.aucma_mes.vm.User;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.ArrayList;
@ -25,6 +23,7 @@ public class HomePageActivity extends AppCompatActivity {
private Intent intent;
private ArrayList<String> roles;
private User user;
private Intent serviceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -37,6 +36,7 @@ public class HomePageActivity extends AppCompatActivity {
String json = intent1.getStringExtra("info");
LoginInfoBeen infoBeen = new Gson().fromJson(json, LoginInfoBeen.class);
InfoDialog infoDialog = new InfoDialog(this);
try {
user = infoBeen.getUser();
@ -87,11 +87,13 @@ public class HomePageActivity extends AppCompatActivity {
startActivity(intent);
});
binding.homeButton6.setOnClickListener(v -> {
// 切换工位
if (!roles.contains("switch_station")) {
Toast.makeText(this, "暂无权限", Toast.LENGTH_SHORT).show();
return;
}
intent = new Intent(this, ChangeActivity.class);
intent.putExtra("title","工位切换");
startActivity(intent);
});
binding.homeButton5.setOnClickListener(v -> {
@ -117,11 +119,25 @@ public class HomePageActivity extends AppCompatActivity {
intent.putExtra("type", true);
startActivity(intent);
});
binding.homeButton9.setOnClickListener(v -> {
// 切换订单
// if (!roles.contains("switch_order")) {
// Toast.makeText(this, "暂无权限", Toast.LENGTH_SHORT).show();
// return;
// }
intent = new Intent(this, ChangeActivity.class);
intent.putExtra("title","切换订单");
startActivity(intent);
});
} catch (Exception e) {
Toast.makeText(this, "暂无权限", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
private String stationName;
@ -130,7 +146,6 @@ public class HomePageActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
stationName = SharedPreferencesUtils.getstring("stationName", user.getStationName());
stationCode = SharedPreferencesUtils.getstring("stationCode", user.getStationCode());
@ -139,7 +154,9 @@ public class HomePageActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
super.onDestroy();
// 重置保存数据
SharedPreferencesUtils.putstring("stationName", null);
SharedPreferencesUtils.putstring("stationCode", null);
stopService(serviceIntent);// 停止服务
}
}

@ -51,7 +51,6 @@ public class MainActivity extends BaseActivity {
user.setPassword("123456");
getSupportActionBar().hide(); // 隐藏原生标题栏
binding.setUser(user);
OkGo.<Result>get(url + "/base/teamMembers/getTeamMemberList").tag(this)
.execute(new MyRecultCall(dialog, this) {
@Override
@ -131,10 +130,10 @@ public class MainActivity extends BaseActivity {
// 登录
public void login(View view) {
if (!loginState) {
Toast.makeText(this, "设备未注册,不允许登录", Toast.LENGTH_SHORT).show();
return;
}
// if (!loginState) {
// Toast.makeText(this, "设备未注册,不允许登录", Toast.LENGTH_SHORT).show();
// return;
// }
String teamCode = user.getTeamCode();
if (teamCode==null||teamCode.isEmpty()) return;
dialog.show();

@ -26,8 +26,8 @@ import okhttp3.MediaType;
public abstract class BaseActivity extends AppCompatActivity {
private MyBroad receiver;
// public static String url="http://192.168.137.1:8080";
public static String url="http://10.100.72.10:8080";
public static String url="http://192.168.137.1:8080";
// public static String url="http://10.100.72.10:8080";
public ProgressDialog dialog;
public Gson gson;
public MediaType JSON = MediaType.parse("application/json; charset=utf-8");

@ -0,0 +1,108 @@
package com.example.aucma_mes.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.example.aucma_mes.R;
import com.example.aucma_mes.base.BaseActivity;
import com.example.aucma_mes.entity.Result;
import com.example.aucma_mes.utils.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
public class MyService extends Service {
private boolean isRunning = true;
private static final int NOTIFICATION_ID = 1;
private Gson gson;
@Override
public void onCreate() {
super.onCreate();
gson=new Gson();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG", "服务启动");
startQuery();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "服务停止");
isRunning = false;
}
private void startQuery() {
Thread queryThread = new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
// 如果查询结果不为空,则发送通知进行提醒
OkGo.<String>post(BaseActivity.url+"/api/messageNotifications")
.params("userName", SharedPreferencesUtils.getstring("loginName",""))
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Result result= gson.fromJson(response.body(), Result.class);
if (result.getCode()==200){
showNotification(result.getMsg(),null);
}
}
});
try {
Thread.sleep(180000); // 设置查询间隔
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
queryThread.start();
}
private void showNotification(String title, String content) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 创建通知渠道适用于Android 8.0及以上版本)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.mipmap.logo2)
.setContentTitle(title)
.setContentText(content)
// .setStyle(new NotificationCompat.BigTextStyle()
// .bigText("这里是非常长的文本内容,可以显示多行文本。"))
.setAutoCancel(true);
// 发送通知
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

@ -4,27 +4,67 @@
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="vm"
type="com.example.aucma_mes.ChangeActivity" />
<variable
name="list"
type="java.util.List" />
<variable
name="title"
type="String" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey"
android:orientation="vertical"
tools:context=".ChangeActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/blue"
android:gravity="center|left"
android:paddingStart="12dp"
android:text="@{title}"
android:textColor="@color/white"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="12dp"
android:focusableInTouchMode="true"
android:focusable="true"
android:visibility='@{title.equals("切换订单")?View.VISIBLE:View.GONE}'
android:layout_marginEnd="4dp">
<TextView
style="@style/text_style1"
android:layout_width="90dp"
android:layout_height="match_parent"
android:text="箱壳条码:" />
<EditText
android:id="@+id/change_boxCode"
style="@style/san_text_style"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginTop="15dp"
android:background="@color/white"
android:layout_marginEnd="4dp">
android:layout_marginEnd="4dp"
android:background="@color/white">
<TextView
style="@style/text_style1"
@ -35,11 +75,11 @@
<Spinner
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@{list}"
android:onItemSelected="@{vm.onItemClick}"/>
android:onItemSelected="@{vm.onItemClick}"
android:textAlignment="center" />
</LinearLayout>
@ -52,6 +92,18 @@
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:onClick="changeSubmit"
android:text="提交"/>
android:text="提交"
android:visibility='@{title.equals("切换订单")?View.GONE:View.VISIBLE}' />
<Button
style="@style/button_true_style"
android:layout_width="370dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:onClick="changeOrderSubmit"
android:text="切换订单"
android:visibility='@{title.equals("切换订单")?View.VISIBLE:View.GONE}' />
</LinearLayout>
</layout>

@ -98,6 +98,14 @@
app:layout_columnWeight="1" />
<RadioButton
android:id="@+id/home_button9"
style="@style/radio_button_style"
android:drawableTop="@mipmap/icon_change2"
android:text="切换订单"
app:layout_columnWeight="1" />
</androidx.gridlayout.widget.GridLayout>
</ScrollView>

Loading…
Cancel
Save