对应这篇文章:433M射频遥控灯、震动感应灯、WIFI避障小车
目录
- 代码概要
- activity_contrl.xml
- ContrlActivity.java
- NetUtils.java
- 详细工程代码下载
代码概要
activity_contrl.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/test"tools:context=".ContrlActivity" ><LinearLayoutandroid:layout_centerVertical="true"android:layout_alignParentRight="true"android:layout_marginRight="100dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><Buttonandroid:id="@+id/btnqian"android:layout_width="100dp"android:layout_height="100dp"android:layout_weight="1"android:background="@drawable/btn_back"android:text="前进"android:layout_margin="10dp"android:textColor="#ffffff" /><Buttonandroid:id="@+id/btnhou"android:layout_width="100dp"android:layout_height="100dp"android:layout_weight="1"android:layout_margin="10dp"android:background="@drawable/btn_back"android:text="后退"android:textColor="#ffffff" /></LinearLayout><LinearLayoutandroid:layout_centerVertical="true"android:layout_marginLeft="100dp"android:layout_height="wrap_content"android:layout_width="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/btnzuo"android:layout_height="100dp"android:layout_width="100dp"android:layout_weight="1"android:background="@drawable/btn_back"android:text="左转"android:layout_margin="10dp"android:textColor="#ffffff" /><Buttonandroid:id="@+id/btnyou"android:layout_height="100dp"android:layout_width="100dp"android:layout_weight="1"android:layout_margin="10dp"android:background="@drawable/btn_back"android:text="右转"android:textColor="#ffffff" /></LinearLayout></RelativeLayout>
ContrlActivity.java
package com.gec.bluetoothcarlink;import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;public class ContrlActivity extends Activity {Button qian;Button hou;Button zuo;Button you;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 璁剧疆鏃犳爣棰樻爮requestWindowFeature(Window.FEATURE_NO_TITLE);// 璁剧疆鍏ㄥ睆getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);// 淇濇寔灞忓箷甯镐寒getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);setContentView(R.layout.activity_contrl);initView();}void initView() {qian = (Button) findViewById(R.id.btnqian);hou = (Button) findViewById(R.id.btnhou);zuo = (Button) findViewById(R.id.btnzuo);you = (Button) findViewById(R.id.btnyou);qian.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View arg0, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:new NetUtils("4").sendMessage();break;case MotionEvent.ACTION_UP: {new NetUtils("s").sendMessage();break;}}return false;}});hou.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View arg0, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:new NetUtils("5").sendMessage();break;case MotionEvent.ACTION_UP: {new NetUtils("s").sendMessage();break;}}return false;}});zuo.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View arg0, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:new NetUtils("6").sendMessage();break;case MotionEvent.ACTION_UP: {new NetUtils("z").sendMessage();break;}}return false;}});you.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View arg0, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:new NetUtils("7").sendMessage();break;case MotionEvent.ACTION_UP: {new NetUtils("z").sendMessage();break;}}return false;}});}}
NetUtils.java
package com.gec.bluetoothcarlink;import java.io.OutputStream;
import java.net.Socket;import android.os.Handler;public class NetUtils {public String message;public String reTurnMes;public Handler handler;public NetUtils(String message) {this.message = message;}public NetUtils(String message, Handler handler) {this.message = message;this.handler = handler;}public void sendMessage() {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Socket client = new Socket("192.168.1.217", 8888);//Socket client = new Socket(StartActivity.IP, Integer.parseInt(StartActivity.Port));OutputStream out = client.getOutputStream();out.write(message.getBytes());out.close();client.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}
}
详细工程代码下载
https://download.csdn.net/download/zhuguanlin121/21023813?spm=1001.2014.3001.5501