Advertisement
vietanhlehuu

UPDATE NEW APP

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