Advertisement
vietanhlehuu

DONE

Oct 25th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. package testing.gps_location;
  2.  
  3. import android.Manifest;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.AsyncTask;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.os.SystemClock;
  13. import android.provider.Settings;
  14. import android.support.annotation.NonNull;
  15. import android.support.annotation.Nullable;
  16. import android.support.v4.app.ActivityCompat;
  17. import android.support.v7.app.AppCompatActivity;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.widget.Button;
  21. import android.widget.TextView;
  22.  
  23. import java.io.BufferedReader;
  24. import java.io.DataInputStream;
  25. import java.io.DataOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.net.DatagramPacket;
  29. import java.net.DatagramSocket;
  30. import java.net.InetAddress;
  31. import java.net.NetworkInterface;
  32. import java.net.Socket;
  33. import java.net.SocketException;
  34. import java.net.UnknownHostException;
  35. import java.nio.channels.DatagramChannel;
  36. import java.util.Date;
  37. import java.util.Enumeration;
  38.  
  39. public class MainActivity extends AppCompatActivity {
  40.  
  41. private Button b;
  42. private TextView t;
  43. private LocationManager locationManager;
  44. private LocationListener listener;
  45. TextView textResponse = null;
  46. String phoneID ="";
  47.  
  48. @Override
  49. protected void onCreate(@Nullable Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51.  
  52. setContentView(R.layout.activity_main);
  53.  
  54. t = (TextView) findViewById(R.id.textView);
  55. b = (Button) findViewById(R.id.button);
  56. phoneID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
  57. textResponse = t;
  58. locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  59.  
  60.  
  61. listener = new LocationListener() {
  62. @Override
  63. public void onLocationChanged(Location location) {
  64. //t.append("\n " + location.getLongitude() + " " + location.getLatitude());
  65. MyUDP myUDP = new MyUDP("192.168.56.1",10000,location.getLongitude(),location.getLatitude() );
  66. myUDP.execute();
  67.  
  68. }
  69.  
  70. @Override
  71. public void onStatusChanged(String s, int i, Bundle bundle) {
  72.  
  73. }
  74.  
  75. @Override
  76. public void onProviderEnabled(String s) {
  77.  
  78. }
  79.  
  80. @Override
  81. public void onProviderDisabled(String s) {
  82.  
  83. Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  84. startActivity(i);
  85. }
  86. };
  87.  
  88. configure_button();
  89.  
  90. MyTCP myClientTask = new MyTCP("192.168.56.1", 5000);
  91. myClientTask.execute();
  92.  
  93.  
  94. }
  95.  
  96. @Override
  97. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  98. switch (requestCode) {
  99. case 10:
  100. configure_button();
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106.  
  107. void configure_button() {
  108. // first check for permissions
  109. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  110. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  111. requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
  112. , 10);
  113. }
  114. return;
  115. }
  116. // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
  117. b.setOnClickListener(new View.OnClickListener() {
  118. @Override
  119. public void onClick(View view) {
  120. //noinspection MissingPermission
  121. locationManager.requestLocationUpdates("gps", 5000, 0, listener);
  122. }
  123. });
  124. }
  125.  
  126. public class MyTCP extends AsyncTask<Void, String, Void> {
  127.  
  128. String dstAddress;
  129. int dstPort;
  130. String response = "";
  131.  
  132. MyTCP(String addr, int port) {
  133. dstAddress = addr;
  134. dstPort = port;
  135. }
  136.  
  137. @Override
  138. protected Void doInBackground(Void... arg0) {
  139.  
  140. Socket socket = null;
  141. DataOutputStream dataOutputStream = null;
  142. //DataInputStream dataInputStream = null;
  143. //while(true) {
  144. try {
  145. socket = new Socket(dstAddress, dstPort);
  146. dataOutputStream = new DataOutputStream(
  147. socket.getOutputStream());
  148. dataOutputStream.writeUTF(phoneID);
  149. //dataInputStream = new DataInputStream(socket.getInputStream());
  150. BufferedReader buff = new BufferedReader(new
  151. InputStreamReader(socket.getInputStream()));
  152. String inputLine= buff.readLine();
  153. while(inputLine != null) {
  154. //khi gọi hàm này thì onProgressUpdate sẽ thực thi
  155. publishProgress(inputLine);
  156. inputLine= buff.readLine();
  157.  
  158. }
  159. } catch (UnknownHostException e) {
  160. e.printStackTrace();
  161. response = "UnknownHostException: " + e.toString();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. response = "IOException: " + e.toString();
  165. } finally {
  166. if (socket != null) {
  167. try {
  168. socket.close();
  169. } catch (IOException e) {
  170. e.printStackTrace();
  171. }
  172. }
  173. if (dataOutputStream != null) {
  174. try {
  175. dataOutputStream.close();
  176. } catch (IOException e) {
  177. // TODO Auto-generated catch block
  178. e.printStackTrace();
  179. }
  180. }
  181.  
  182. }
  183. // SystemClock.sleep(5000);
  184. //}
  185.  
  186. return null;
  187. }
  188. @Override
  189. protected void onProgressUpdate(String... values) {
  190. // TODO Auto-generated method stub
  191. super.onProgressUpdate(values);
  192. textResponse.append("\n" + values[0]);
  193. if(response != "") {
  194. textResponse.setText(response);
  195. response ="";
  196. }
  197. }
  198. @Override
  199. protected void onPostExecute(Void result) {
  200. super.onPostExecute(result);
  201. //textResponse.append("\n" + response);
  202. }
  203.  
  204. }
  205.  
  206. public class MyUDP extends AsyncTask<Void, Void, Void> {
  207.  
  208. String dstAddress;
  209. int dstPort;
  210. String response = "";
  211. String message = "This is my message";
  212.  
  213. MyUDP(String addr, int port,double x, double y) {
  214. dstAddress = addr;
  215. dstPort = port;
  216. java.util.Date date=new Date();
  217. message = phoneID +"@" + String.format("%.4f", x) + "@" +String.format("%.4f", y) + "@" + date.getTime();
  218. }
  219.  
  220. @Override
  221. protected Void doInBackground(Void... arg0) {
  222.  
  223. try {
  224. //Preparing the socket
  225. InetAddress serverAddr = InetAddress.getByName(dstAddress);
  226. DatagramSocket socket = new DatagramSocket();
  227. //Preparing the packet
  228. byte[] buf = message.getBytes();
  229. DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, dstPort);
  230.  
  231.  
  232. socket.send(packet);
  233. } catch (Exception e) {
  234. response = e.toString();
  235. }
  236.  
  237. return null;
  238. }
  239.  
  240. @Override
  241. protected void onPostExecute(Void result) {
  242. super.onPostExecute(result);
  243. textResponse.append("\n" + response);
  244. }
  245.  
  246. }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement