From 35640bee7ee5469b92f508c3887983f2f9b8ab94 Mon Sep 17 00:00:00 2001 From: neardws Date: Fri, 21 Sep 2018 22:33:58 +0800 Subject: [PATCH 1/2] second exam --- .../MainActivity.java | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java index 2e42711..7253d1b 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java @@ -460,6 +460,11 @@ public void onReceive(Context context, Intent intent) { switch (type){ case Result.TYPE_RESULT: warning = result.isWarning(); + if (warning){ + getWarning(); + } else { + getSafe(); + } distance = result.getDistance(); time = result.getTime(); sendTimeStamp = result.getSendTimeStamp(); @@ -468,11 +473,6 @@ public void onReceive(Context context, Intent intent) { tv_warning.setText(String.valueOf(warning)); tv_distance.setText(String.valueOf(distance)); tv_time.setText(String.valueOf(time)); - if (warning){ - getWarning(); - } else { - getSafe(); - } long delay = nowTimwStamp - sendTimeStamp; addResultToDB(id, time, distance, warning, nowTimwStamp, sendTimeStamp, delay); break; @@ -499,24 +499,24 @@ public void onReceive(Context context, Intent intent) { break; case InfoThread.ACTION_INFORMATION: Message intentMessage = (Message) Objects.requireNonNull(intent.getExtras()).get(InfoThread.EXTRAR_INFORMATION); - if (intentMessage.getId() != Message.ERROR_VALUE){ - et_id.setText(String.valueOf(intentMessage.getId())); - } - if (intentMessage.getAce() != Message.ERROR_VALUE){ - et_ace.setText(String.valueOf(intentMessage.getAce())); - } - if (intentMessage.getDirection() != Message.ERROR_VALUE){ - et_direction.setText(String.valueOf(intentMessage.getDirection())); - } - if (intentMessage.getLat() != Message.ERROR_VALUE){ - et_lat.setText(String.valueOf(intentMessage.getLat())); - } - if (intentMessage.getLon() != Message.ERROR_VALUE){ - et_lon.setText(String.valueOf(intentMessage.getLon())); - } - if (intentMessage.getSpeed() != Message.ERROR_VALUE){ - et_speed.setText(String.valueOf(intentMessage.getSpeed())); - } +// if (intentMessage.getId() != Message.ERROR_VALUE){ +// et_id.setText(String.valueOf(intentMessage.getId())); +// } +// if (intentMessage.getAce() != Message.ERROR_VALUE){ +// et_ace.setText(String.valueOf(intentMessage.getAce())); +// } +// if (intentMessage.getDirection() != Message.ERROR_VALUE){ +// et_direction.setText(String.valueOf(intentMessage.getDirection())); +// } +// if (intentMessage.getLat() != Message.ERROR_VALUE){ +// et_lat.setText(String.valueOf(intentMessage.getLat())); +// } +// if (intentMessage.getLon() != Message.ERROR_VALUE){ +// et_lon.setText(String.valueOf(intentMessage.getLon())); +// } +// if (intentMessage.getSpeed() != Message.ERROR_VALUE){ +// et_speed.setText(String.valueOf(intentMessage.getSpeed())); +// } if (intentMessage.getTimeStamp() != Message.ERROR_VALUE){ if (0 != getBaseStamp() && 0 != getServerStamp()){ tvPingResult.setText("Time synced."); @@ -541,12 +541,12 @@ public void onReceive(Context context, Intent intent) { /**** * No,2 */ -// et_id.setText("-801736957"); -// et_speed.setText("1.48688"); -// et_ace.setText("-0.01"); -// et_lat.setText("29.598302"); -// et_lon.setText("106.295269"); -// et_direction.setText("-74.463"); + et_id.setText("-801736957"); + et_speed.setText("1.48688"); + et_ace.setText("-0.01"); + et_lat.setText("28.598280"); + et_lon.setText("106.295269"); + et_direction.setText("-74.463"); break; case MainActivity.ACTION_SEND_MESSAGE: @@ -621,7 +621,7 @@ private void addMessageToDB(){ if (lat.equals("")){ intent.putExtra(DBTool.MESSAGE_LAT, 666); } else { - intent.putExtra(DBTool.MESSAGE_LON, Double.parseDouble(lat)); + intent.putExtra(DBTool.MESSAGE_LAT, Double.parseDouble(lat)); } if (lon.equals("")){ intent.putExtra(DBTool.MESSAGE_LON, 666); @@ -729,7 +729,7 @@ private void sendTimeStampMessage(){ public void getWarning(){ img_warning.setImageDrawable(getResources().getDrawable(R.mipmap.ic_danger)); - Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); + Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone mRingtone = RingtoneManager.getRingtone(getApplicationContext(),uri); mRingtone.play(); From d9c1fa8e35ca7273e252c392342f848281139c51 Mon Sep 17 00:00:00 2001 From: neardws Date: Thu, 27 Sep 2018 15:48:32 +0800 Subject: [PATCH 2/2] add --- .../collision_warning_system/DAO/Message.java | 2 + .../collision_warning_system/DAO/Result.java | 1 + .../MainActivity.java | 283 ++++++++++++++---- .../database/DBTool.java | 2 +- .../udp/SendService.java | 59 +++- APP/app/src/main/res/layout/activity_main.xml | 4 +- 6 files changed, 284 insertions(+), 67 deletions(-) diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Message.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Message.java index ad3e938..1f8180a 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Message.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Message.java @@ -8,6 +8,8 @@ public class Message implements Serializable { public static final String TYPE_MESSAGE = "TYPE_MESSAGE"; public static final String TYPE_TIME_SYNC_MESSAGE = "TYPE_TIME_SYNC_MESSAGE"; + public static final String TYPE_WIFI_TIME_SYNC_RESULT = "TYPE_WIFI_TIME_SYNC_RESULT"; + public static final String TYPE_WIFI_MESSAGE = "TYPE_WIFI_MESSAGE"; public static int ERROR_VALUE = -666; private int id; private long timeStamp; diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Result.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Result.java index df66f91..fb1cbd7 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Result.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/DAO/Result.java @@ -39,6 +39,7 @@ public class Result implements Serializable { public static final String TYPE_RESULT = "TYPE_RESULT"; public static final String TYPE_TIME_SYNC_RESULT = "TYPE_TIME_SYNC_RESULT"; + public static final String TYPE_WIFI_TIME_SYNC = "TYPE_WIFI_TIME_SYNC"; public static int ERROR_VALUE = -666; private int id; private int time; diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java index 7253d1b..51369b2 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/MainActivity.java @@ -36,9 +36,13 @@ import com.raizlabs.android.dbflow.list.IFlowCursorIterator; import java.io.IOException; +import java.io.PrintStream; +import java.io.SyncFailedException; +import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Objects; +import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -65,6 +69,8 @@ public class MainActivity extends AppCompatActivity { public static final String ACTION_SEND_MESSAGE = "ACTION_SEND_MESSAGE"; public static final String ACTION_SQL = "ACTION_SQL"; + public static final String ACTION_LTE_TIME_SYNC = "ACTION_LTE_TIME_SYNC"; + private static final String SERVER_IP = "192.168.1.80"; private static final String SERVER_PORT = "4040"; @@ -152,7 +158,7 @@ protected void onCreate(Bundle savedInstanceState) { intentFilter.addAction(InfoThread.ACTION_INFORMATION); intentFilter.addAction(MainActivity.ACTION_SEND_MESSAGE); intentFilter.addAction(MainActivity.ACTION_SQL); - + intentFilter.addAction(MainActivity.ACTION_LTE_TIME_SYNC); etIp = (EditText) findViewById(R.id.et_ipAdd); btnPing = (Button) findViewById(R.id.btn_testPing); @@ -177,7 +183,6 @@ protected void onCreate(Bundle savedInstanceState) { tv_gps = (TextView) findViewById(R.id.tv_gps); tv_sql = (TextView) findViewById(R.id.tv_sql); - img_warning = (ImageView) findViewById(R.id.img_warning); img_warning.setImageDrawable(getResources().getDrawable(R.mipmap.ic_safe)); @@ -186,60 +191,6 @@ protected void onCreate(Bundle savedInstanceState) { btnQuery = (Button) findViewById(R.id.btn_query); btnBackup = (Button) findViewById(R.id.btn_backup); - btnPing.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (etIp.getEditableText().toString().equals("")){ //使用默认IP地址 - if (isConnectedServer(SERVER_IP)){ - tvPingResult.setText("Ping "+SERVER_IP+ "Success."); - addLogToDB("Ping "+SERVER_IP+ "Success."); - } else { - tvPingResult.setText("Ping "+SERVER_IP+ "Failed."); - addLogToDB("Ping "+SERVER_IP+ "Failed."); - } - } else { - String ipAdd = (String) etIp.getEditableText().toString(); - if (isIp(ipAdd)){ //ip匹配 - setServer_Add(ipAdd); - if (isConnectedServer(ipAdd)){ - tvPingResult.setText("Ping"+ipAdd+ "Success."); - addLogToDB("Ping"+ipAdd+ "Success."); - } else { - tvPingResult.setText("Ping"+ipAdd+ "Failed."); - addLogToDB("Ping"+ipAdd+ "Failed."); - } - } else { //ip不匹配 - tvPingResult.setText("请重新输入IP."); - addLogToDB("请重新输入IP."); - } - - } - - } - }); - - btnBackup.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - addLogToDB("备份数据库"); - Intent intent = new Intent(MainActivity.this, DBTool.class); - intent.setAction(DBTool.ACTION_COPY); - startService(intent); - } - }); - - /*********************************************** - * 现为时间同步按钮 - */ - btnSend.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) {//只要一点发送,就有数据的录入,获取,转换,以及数据json格式的转化过程 - sendTimeStampMessage(); - addLogToDB("发起时间同步"); - } - }); - - /** * 注册接收线程、发送线程 */ @@ -308,6 +259,58 @@ public void onClick(View view) {//只要一点发送,就有数据的录入, infoThread.start(); + btnPing.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (etIp.getEditableText().toString().equals("")){ //使用默认IP地址 + if (isConnectedServer(SERVER_IP)){ + tvPingResult.setText("Ping "+SERVER_IP+ "Success."); + addLogToDB("Ping "+SERVER_IP+ "Success."); + } else { + tvPingResult.setText("Ping "+SERVER_IP+ "Failed."); + addLogToDB("Ping "+SERVER_IP+ "Failed."); + } + } else { + String ipAdd = (String) etIp.getEditableText().toString(); + if (isIp(ipAdd)){ //ip匹配 + setServer_Add(ipAdd); + if (isConnectedServer(ipAdd)){ + tvPingResult.setText("Ping"+ipAdd+ "Success."); + addLogToDB("Ping"+ipAdd+ "Success."); + } else { + tvPingResult.setText("Ping"+ipAdd+ "Failed."); + addLogToDB("Ping"+ipAdd+ "Failed."); + } + } else { //ip不匹配 + tvPingResult.setText("请重新输入IP."); + addLogToDB("请重新输入IP."); + } + + } + + } + }); + + btnBackup.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + addLogToDB("备份数据库"); + Intent intent = new Intent(MainActivity.this, DBTool.class); + intent.setAction(DBTool.ACTION_COPY); + startService(intent); + } + }); + + /*********************************************** + * 现为时间同步按钮 + */ + btnSend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) {//只要一点发送,就有数据的录入,获取,转换,以及数据json格式的转化过程 + sendTimeStampMessage(); + addLogToDB("发起时间同步"); + } + }); btnStart.setOnClickListener(new View.OnClickListener() { @Override @@ -341,7 +344,6 @@ public void onClick(View v) { } public boolean isConnectedServer(String ip) { - Runtime runtime = Runtime.getRuntime(); try { @@ -490,6 +492,32 @@ public void onReceive(Context context, Intent intent) { default: break; } + } else if (String.valueOf(id).equals("-666")){ + String type = result.getType(); + long sendTimeStamp = result.getSendTimeStamp(); + long timeStamp = System.currentTimeMillis(); + if (type.equals(Result.TYPE_WIFI_TIME_SYNC)){ + Message messageWifiTimeSync = new Message(); + messageWifiTimeSync.setId(Message.ERROR_VALUE); + messageWifiTimeSync.setType(Message.TYPE_WIFI_TIME_SYNC_RESULT); + messageWifiTimeSync.setLat(Message.ERROR_VALUE); + messageWifiTimeSync.setLon(Message.ERROR_VALUE); + messageWifiTimeSync.setAce(sendTimeStamp); + messageWifiTimeSync.setDirection(Message.ERROR_VALUE); + messageWifiTimeSync.setSpeed(Message.ERROR_VALUE); + // messageWifiTimeSync.setMac(String.valueOf(sendTimeStamp)); + messageWifiTimeSync.setTimeStamp(timeStamp); + tvPingResult.setText("Type wifi time sync"); + Intent sendIntent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动 + sendIntent.setAction(SendService.ACTION_SEND_JSON);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。 + sendIntent.putExtra(SendService.EXTRAS_HOST,"192.168.1.85");//将执行服务活动的限制,IP地址,端口号,还有 + sendIntent.putExtra(SendService.EXTRAS_PORT,"4040"); + sendIntent.putExtra(SendService.EXTRAS_JSON,messageWifiTimeSync); + startService(sendIntent);//现在真正执行服务 + + }else { + tvPingResult.setText("Result is from else id:"+String.valueOf(result.getId())+" "+type); + } } else { tvPingResult.setText("Result is from else id:"+String.valueOf(result.getId())); } @@ -548,6 +576,16 @@ public void onReceive(Context context, Intent intent) { et_lon.setText("106.295269"); et_direction.setText("-74.463"); break; + case MainActivity.ACTION_LTE_TIME_SYNC: + long sendTimeStamp = intent.getExtras().getLong("sendTimeStamp"); + long receiverTimeStamp = intent.getExtras().getLong("receiverTimeStamp"); + long timeStamp = intent.getExtras().getLong("timeStamp"); + long delay = (sendTimeStamp - receiverTimeStamp) /2; + long serverTimeStamp = timeStamp + delay; + setServerStamp(serverTimeStamp); + setBaseStamp(receiverTimeStamp); + tvPingResult.setText("Time Sync success"); + break; case MainActivity.ACTION_SEND_MESSAGE: break; @@ -641,6 +679,12 @@ private void addMessageToDB(){ startService(intent); } + + /************************************ + * + * 发送Message, 给RSU, 给云服务器,给连接AP的PC + * + ********************************/ private void sendMessage(){ String id = et_id.getEditableText().toString();//将输入的格式统一化为string型 String timeStamp = et_timeStamp.getEditableText().toString(); @@ -679,6 +723,127 @@ private void sendMessage(){ intent.putExtra(SendService.EXTRAS_JSON,message); startService(intent);//现在真正执行服务 + } + private void sendMessageToLTE(){ + String id = et_id.getEditableText().toString();//将输入的格式统一化为string型 + String timeStamp = et_timeStamp.getEditableText().toString(); + String speed = et_speed.getEditableText().toString(); + String direction = et_direction.getEditableText().toString(); + String lat = et_lat.getEditableText().toString(); + String lon = et_lon.getEditableText().toString(); + String ace = et_ace.getEditableText().toString(); + + Message message = new Message(); + message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型 + message.setTimeStamp(Long.valueOf(timeStamp)); + if (!speed.equals("")){ + message.setSpeed(Float.valueOf(speed)); + } + if (!direction.equals("")){ + message.setDirection(Float.valueOf(direction)); + } + if (!lat.equals("")){ + message.setLat(Double.valueOf(lat)); + } + if (!lon.equals("")){ + message.setLon(Double.valueOf(lon)); + } + if (!ace.equals("")){ + message.setAce(Double.valueOf(ace)); + } + message.setType(Message.TYPE_MESSAGE); //普通消息 + //现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。 + + + Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动 + intent.setAction(SendService.ACTION_SEND_TCP);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。 + intent.putExtra(SendService.EXTRAS_HOST,"120.78.167.211");//将执行服务活动的限制,IP地址,端口号,还有 + //intent.putExtra(SendService.EXTRAS_HOST,"192.168.123.186");//将执行服务活动的限制,IP地址,端口号,还有 + intent.putExtra(SendService.EXTRAS_PORT,"4040"); + intent.putExtra(SendService.EXTRAS_JSON,message); + startService(intent);//现在真正执行服务 + + } + + private void sendMessageToLTETimeSync(){ + String id = et_id.getEditableText().toString();//将输入的格式统一化为string型 + String timeStamp = et_timeStamp.getEditableText().toString(); + String speed = et_speed.getEditableText().toString(); + String direction = et_direction.getEditableText().toString(); + String lat = et_lat.getEditableText().toString(); + String lon = et_lon.getEditableText().toString(); + String ace = et_ace.getEditableText().toString(); + + Message message = new Message(); + message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型 + message.setTimeStamp(Long.valueOf(timeStamp)); + if (!speed.equals("")){ + message.setSpeed(Float.valueOf(speed)); + } + if (!direction.equals("")){ + message.setDirection(Float.valueOf(direction)); + } + if (!lat.equals("")){ + message.setLat(Double.valueOf(lat)); + } + if (!lon.equals("")){ + message.setLon(Double.valueOf(lon)); + } + if (!ace.equals("")){ + message.setAce(Double.valueOf(ace)); + } + message.setType(Message.TYPE_TIME_SYNC_MESSAGE); //普通消息 + //现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。 + + + Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动 + intent.setAction(SendService.ACTION_SEND_TCP);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。 + intent.putExtra(SendService.EXTRAS_HOST,"120.78.167.211");//将执行服务活动的限制,IP地址,端口号,还有 + //intent.putExtra(SendService.EXTRAS_HOST,"192.168.123.186");//将执行服务活动的限制,IP地址,端口号,还有 + intent.putExtra(SendService.EXTRAS_PORT,"4040"); + intent.putExtra(SendService.EXTRAS_JSON,message); + startService(intent);//现在真正执行服务 + + } + + private void sendMessageToWIFI(){ + String id = et_id.getEditableText().toString();//将输入的格式统一化为string型 + String timeStamp = String.valueOf(System.currentTimeMillis()); + String speed = et_speed.getEditableText().toString(); + String direction = et_direction.getEditableText().toString(); + String lat = et_lat.getEditableText().toString(); + String lon = et_lon.getEditableText().toString(); + String ace = et_ace.getEditableText().toString(); + + Message message = new Message(); + message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型 + message.setTimeStamp(Long.valueOf(timeStamp)); + if (!speed.equals("")){ + message.setSpeed(Float.valueOf(speed)); + } + if (!direction.equals("")){ + message.setDirection(Float.valueOf(direction)); + } + if (!lat.equals("")){ + message.setLat(Double.valueOf(lat)); + } + if (!lon.equals("")){ + message.setLon(Double.valueOf(lon)); + } + if (!ace.equals("")){ + message.setAce(Double.valueOf(ace)); + } + message.setType(Message.TYPE_WIFI_MESSAGE); //普通消息 + //现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。 + + + Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动 + intent.setAction(SendService.ACTION_SEND_JSON);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。 + intent.putExtra(SendService.EXTRAS_HOST,"192.168.1.85");//将执行服务活动的限制,IP地址,端口号,还有 + intent.putExtra(SendService.EXTRAS_PORT,"4040"); + intent.putExtra(SendService.EXTRAS_JSON,message); + startService(intent);//现在真正执行服务 + } /************************************ @@ -757,7 +922,9 @@ public void run() { stop = false; while (!stop){ try { - sendMessage(); + // sendMessage(); + // sendMessageToWIFI(); + sendMessageToLTETimeSync(); addMessageToDB(); sleep(FREQUENCY); } catch (InterruptedException e) { diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/database/DBTool.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/database/DBTool.java index bb70912..267ba68 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/database/DBTool.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/database/DBTool.java @@ -39,7 +39,7 @@ public DBTool(){ super("DBTool"); } public static final String TAG = "DBTool"; - public static final String DATABASE_PATH = "/mnt/sdcard/Downloads/"; + public static final String DATABASE_PATH = "/mnt/sdcard/Download/"; public static final String DATABASE_FLIENAME = "databasebackup.db"; public static final String ACTION_ADD = "ACTION_ADD"; diff --git a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/udp/SendService.java b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/udp/SendService.java index f129613..88442cb 100644 --- a/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/udp/SendService.java +++ b/APP/app/src/main/java/com/github/cqu_bdsc/collision_warning_system/udp/SendService.java @@ -7,13 +7,21 @@ import android.support.annotation.Nullable; import com.github.cqu_bdsc.collision_warning_system.DAO.Message; +import com.github.cqu_bdsc.collision_warning_system.MainActivity; import org.json.JSONObject; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; @@ -23,6 +31,7 @@ public class SendService extends IntentService {//继承父类IntentService public static final String ACTION_SEND_STRING = "SEND_STRING"; public static final String ACTION_SEND_JSON = "SEND_JSON"; + public static final String ACTION_SEND_TCP = "SEND_TCP"; public static final String EXTRAS_HOST = "EXTRAS_HOST";//主机 public static final String EXTRAS_PORT = "EXTRAS_PORT";//端口 @@ -75,10 +84,6 @@ protected void onHandleIntent(@Nullable Intent intent) { udpSocket.close(); } } - - - - } else if (action.equals(SendService.ACTION_SEND_JSON)){ Message message = (Message) intent.getExtras().get(EXTRAS_JSON); JSONObject jsonObject = message.toJSON(); @@ -98,8 +103,50 @@ protected void onHandleIntent(@Nullable Intent intent) { udpSocket.close(); } } - } - else { + } else if (action.equals(SendService.ACTION_SEND_TCP)){ + Message message = (Message) intent.getExtras().get(EXTRAS_JSON); + JSONObject jsonObject = message.toJSON(); + long sendTimeStamp, receiverTimeStamp, timeStamp = -666; + try { + sendTimeStamp = System.currentTimeMillis(); + //1.创建客户端Socket,指定服务器地址和端口 + Socket socket=new Socket("120.78.167.211", 4040); + //2.获取输出流,向服务器端发送信息 + OutputStream os=socket.getOutputStream();//字节输出流 + PrintWriter pw=new PrintWriter(os);//将输出流包装为打印流 + byte[] buff = (byte[]) jsonObject.toString().getBytes("UTF-8"); + String str = new String(buff); + pw.write(str); + pw.flush(); + socket.shutdownOutput();//关闭输出流 + //3.获取输入流,并读取服务器端的响应信息 + InputStream is=socket.getInputStream(); + BufferedReader br=new BufferedReader(new InputStreamReader(is)); + String info=null; + while((info=br.readLine())!=null){ + timeStamp = Long.parseLong(info); + System.out.println("我是客户端,服务器说:"+info); + } + receiverTimeStamp = System.currentTimeMillis(); + Intent actionIntent = new Intent(); + actionIntent.setAction(MainActivity.ACTION_LTE_TIME_SYNC); + actionIntent.putExtra("sendTimeStamp", sendTimeStamp); + actionIntent.putExtra("receiverTimeStamp", receiverTimeStamp); + actionIntent.putExtra("timeStamp", timeStamp); + sendBroadcast(actionIntent); + //4.关闭资源 + br.close(); + is.close(); + pw.close(); + os.close(); + socket.close(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + } } catch (UnknownHostException e) { e.printStackTrace(); diff --git a/APP/app/src/main/res/layout/activity_main.xml b/APP/app/src/main/res/layout/activity_main.xml index 8737073..c5f2343 100644 --- a/APP/app/src/main/res/layout/activity_main.xml +++ b/APP/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> - @@ -275,6 +275,6 @@ - + \ No newline at end of file