优化VideoActivity、MainActivity逻辑,符合用户使用习惯

chiyanzhen_branch
CYZ 2 years ago
parent 30b6dbde0c
commit db545bbb14

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\zqy04\.android\avd\Pixel_XL_API_31.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-06-25T00:54:24.006491100Z" />
</component>
</project>

@ -0,0 +1,10 @@
k java:S117'"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ß<>±÷üÿÿÿÿ
q
java:S1450"[Remove the "mLoginButton" field and declare it as a local variable in the relevant methods.(žÉÕ<C389>
t
java:S1171"YMove the contents of this initializer to a standard constructor or to field initializers.(ÒÌâ–øÿÿÿÿ
G
java:S1602 ",Remove useless curly braces around statement(òã‘õúÿÿÿÿ
V
java:S11263"AReplace this if-then-else statement by a single return statement.(Ö<>¼z

@ -23,3 +23,5 @@ j
:app/src/main/java/com/example/sixaunyi/DetectActivity.java,9\6\9668392b13e33166876f2a7d3f10eb5405e757ad
k
;app/src/main/java/com/example/sixaunyi/ControlActivity.java,c\9\c99a584c2117ed2df42496451bb97ac1147f5ea0
i
9app/src/main/java/com/example/sixaunyi/LoginActivity.java,f\7\f7730cde41931660f06cfc32899edf44acf4d104

@ -52,8 +52,11 @@ import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
@ -107,6 +110,9 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
private float distance;
private float MonitorDistance;
private UdpReceiveThread udpReceiveThread;
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private final static String SEND_to_IP = "192.168.39.47";
private final static int SEND_PORT = 8888;
@SuppressLint("MissingInflatedId")
@Override
@ -117,6 +123,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
ClearMarker_btn = findViewById(R.id.clearMarker_btn);
ChangeAct_btn = findViewById(R.id.change_btn);
ChangeAct_btn.show();
//tvContent = findViewById(R.id.tv_content);
/*,
*
@ -397,9 +404,12 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
public void changeAct(View view) {
udpReceiveThread.stopListening();
Intent intent = new Intent(MainActivity.this, VideoActivity.class);
Bundle b = new Bundle();
b.putString("Angle", MonitorAngle);
b.putFloat("Distance", MonitorDistance);
intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
/**
@ -490,8 +500,8 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
private boolean mRunning;
private String mReceiveIp = Recieve_IP;
private String mExpectedData = "Motion detected!";
private float mMonitorDistance = MonitorDistance;
private String mMonitorAngle = MonitorAngle;
private float udpMonitorDistance = MonitorDistance;
private String udpMonitorAngle = MonitorAngle;
@Override
public void run() {
@ -505,13 +515,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
String receivedString = new String(data, 0, packet.getLength());
Log.d(TAG, "Received data: " + receivedString);
if (receivedString.equals(mExpectedData)) {
Intent intent = new Intent(MainActivity.this, VideoActivity.class);
Bundle b = new Bundle();
b.putString("Angle", mMonitorAngle);
b.putFloat("Distance", mMonitorDistance);
intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
sendCommand("WARNING");
mRunning = false;
}
}
@ -591,5 +595,40 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
}
return String.valueOf(angle);
}
//UDP发送线程
class SendRunnable implements Runnable {
byte[] mData;
InetAddress mAddress;
int mPort;
public SendRunnable(byte[] data, InetAddress address, int port) {
mData = data;
mAddress = address;
mPort = port;
}
@Override
public void run() {
try {
// 创建发送数据报文
DatagramPacket packet = new DatagramPacket(mData, mData.length, mAddress, mPort);
// 创建 DatagramSocket 对象并发送数据报文
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
// 关闭 DatagramSocket 对象
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//封装好的接口函数
private void sendCommand(String str) throws UnknownHostException {
byte[] sendData = str.getBytes();
InetAddress address = InetAddress.getByName(SEND_to_IP);
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
mThreadPool.execute(sendRunnable1);
}
}

@ -27,6 +27,7 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
@ -43,6 +44,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.os.Bundle;
import android.widget.ToggleButton;
public class VideoActivity extends AppCompatActivity {
private final static String SEND_to_IP = "192.168.39.47"; //填写接收方IP
@ -71,6 +73,7 @@ public class VideoActivity extends AppCompatActivity {
private ImageButton Setting_btn;
private ImageButton TakeOff_btn;
private ImageButton Landing_btn;
private ToggleButton Mode_btn;
private String mangle;
private float mdistance;
// 创建一个可缓存的线程池
@ -85,7 +88,6 @@ public class VideoActivity extends AppCompatActivity {
setContentView(R.layout.activity_control);
imgShow = findViewById(R.id.imageView);
Forward_btn = findViewById(R.id.forward_button);
Back_btn = findViewById(R.id.backward_button);
Left_btn = findViewById(R.id.left_button);
Right_btn = findViewById(R.id.right_button);
@ -95,6 +97,19 @@ public class VideoActivity extends AppCompatActivity {
Landing_btn = findViewById(R.id.landing_button);
TurnRight_btn = findViewById(R.id.rotate_right_button);
TakeOff_btn = findViewById(R.id.takeoff_button);
Mode_btn = findViewById(R.id.toggleButton2);
Setting_btn = findViewById(R.id.setting_button);
Intent i = getIntent();
mdistance = i.getFloatExtra("Distance",0);
mangle = i.getStringExtra("Angle");
try {
Condition_Init();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
private void Control_Button_Init(){
TakeOff_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -109,25 +124,14 @@ public class VideoActivity extends AppCompatActivity {
Landing_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ReceiveHandler receiveHandler = new ReceiveHandler();
UdpReceiveThread StreamThread = new UdpReceiveThread(Recieve_IP,RECEIVE_PORT,receiveHandler);
try {
StreamThread.startListen();
StreamThread.start();
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
sendCommand("LAND");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
Log.i(TAG, "onClick: LLLLLLLLLLLLLLLLLLand");
}
});
Setting_btn = findViewById(R.id.setting_button);
Button_Init();
Intent i = getIntent();
mdistance = i.getFloatExtra("Distance",0);
mangle = i.getStringExtra("Angle");
}
private void Button_Init(){
Forward_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -224,6 +228,48 @@ public class VideoActivity extends AppCompatActivity {
}
});
}
private void Condition_Init() throws UnknownHostException {
sendCommand("AUTO");
ReceiveHandler receiveHandler = new ReceiveHandler();
UdpReceiveThread StreamThread = new UdpReceiveThread(Recieve_IP,RECEIVE_PORT,receiveHandler);
try {
StreamThread.startListen();
StreamThread.start();
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
}
Mode_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// 执行功能2
try {
changetoManual();
Control_Button_Init();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
} else {
// 执行功能1
try {
changetoAuto();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
});
}
private void changetoAuto() throws UnknownHostException {
sendCommand("AUTO");
}
private void changetoManual() throws UnknownHostException {
sendCommand("MAN");
}
class ReceiveHandler extends Handler{
@Override
public void handleMessage(Message msg) {
@ -280,11 +326,9 @@ public class VideoActivity extends AppCompatActivity {
byte[] inBuf = new byte[1024 * 1024];
DatagramPacket inPacket = new DatagramPacket(inBuf, inBuf.length);
reveSocket.receive(inPacket);
if (!inPacket.getAddress().equals(serverAddr)) {
throw new IOException("未知名的报文");
}
ByteArrayInputStream in = new ByteArrayInputStream(inPacket.getData());
receiveHandler.sendEmptyMessage(1);
bp = BitmapFactory.decodeStream(in);
@ -333,34 +377,7 @@ public class VideoActivity extends AppCompatActivity {
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
mThreadPool.execute(sendRunnable1);
}
/*
* UDP线
* */
public static class UdpSendThread extends Thread {
private InetAddress sendToIP; // 目标 IP 地址
private int sendPort; // 目标端口号
private byte[] sendData; // 要发送的数据
public UdpSendThread(InetAddress sendToIP, int sendPort, byte[] sendData) {
this.sendToIP = sendToIP;
this.sendPort = sendPort;
this.sendData = sendData;
}
@Override
public void run() {
try {
DatagramSocket sendSocket = new DatagramSocket(); // 创建一个新的 DatagramSocket 对象
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, sendToIP, sendPort); // 将要发送的数据打包成一个 DatagramPacket 对象
sendSocket.send(sendPacket); // 发送 DatagramPacket
sendSocket.close(); // 发送完毕后关闭 DatagramSocket
} catch (Exception e) {
e.printStackTrace();
}
}
}
//初始化UDP发送线程
// private static void sendCommand(String Sendstr) throws UnknownHostException {
// InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/auto" android:state_checked="false" /> <!-- 当未选中时的背景 -->
<item android:drawable="@drawable/manual" android:state_checked="true" /> <!-- 当选中时的背景 -->
</selector>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/zidongkongzhi" android:state_checked="false" /> <!-- 当未选中时的图标 -->
<item android:drawable="@drawable/shoudong" android:state_checked="true" /> <!-- 当选中时的图标 -->
</selector>

@ -61,7 +61,7 @@
android:background="@null"
android:contentDescription="@string/turn_left_button"
android:src="@drawable/turnleft"
android:scaleType="centerCrop"
android:scaleType="centerInside"
/>
<!-- 右旋转按钮 -->
@ -230,6 +230,24 @@
/>
</LinearLayout>
<LinearLayout
android:id="@+id/top_right_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_gravity="right|top">
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="2"
android:background="@drawable/selector_toggle_icon"
android:text="ToggleButton"
android:textOff=""
android:textOn="" />
</LinearLayout>
</FrameLayout>

Loading…
Cancel
Save