Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  6.  
  7. }
  8.  
  9. protected void onResume() {
  10. super.onResume();
  11.  
  12. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  13. // TODO: Consider calling
  14. // ActivityCompat#requestPermissions
  15. // here to request the missing permissions, and then overriding
  16. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  17. // int[] grantResults)
  18. // to handle the case where the user grants the permission. See the documentation
  19. // for ActivityCompat#requestPermissions for more details.
  20. return;
  21. }
  22. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER/*ТИП ПРОВАЙДЕРА*/,// НА ВХОД ЕМУ ПОДАЁМ
  23. 1000 * 10/*МИНИМАЛЬНОЕ ВРЕМЯ ЗАПРОСА КООРДИНАТ*/, 10/*РАСТОЯНИЕ ОТОЙДЯ НА КОТОРОЕ ОБНОВЛЯЮТСЯ КООРДИНАТЫ*/, locationListener);
  24. }
  25.  
  26. @Override
  27. protected void onPause() {//ОТКЛЮЧАЕМ СЛУШАТЕЛЯ МЕТОДА removeUpdates
  28. super.onPause();
  29. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  30. // TODO: Consider calling
  31. // ActivityCompat#requestPermissions
  32. // here to request the missing permissions, and then overriding
  33. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  34. // int[] grantResults)
  35. // to handle the case where the user grants the permission. See the documentation
  36. // for ActivityCompat#requestPermissions for more details.
  37. return;
  38. }
  39. locationManager.removeUpdates(locationListener);
  40. }
  41.  
  42. private LocationListener locationListener = new LocationListener() {
  43.  
  44. @Override
  45. public void onLocationChanged(Location location) {
  46. showLocation(location);
  47. coordinatesGPS ();
  48. }
  49.  
  50. @Override
  51. public void onStatusChanged(String provider, int status, Bundle extras) {
  52.  
  53. }
  54.  
  55. @Override
  56. public void onProviderDisabled(String provider) {
  57. checkDisabled1();
  58. checkDisabled2();
  59. }
  60.  
  61. @Override
  62. public void onProviderEnabled(String provider) {
  63.  
  64. }
  65.  
  66. };
  67.  
  68. private void showLocation(Location location) {
  69. if (location == null)
  70. return;
  71.  
  72. dataGPS1 = formatLocation1(location);
  73. dataGPS2 = formatLocation2(location);
  74. }
  75.  
  76. public String formatLocation1(Location location) {
  77. if (location == null)
  78. return "";
  79. return String.format(
  80. "%1$.4f",
  81. location.getLatitude(), location.getLongitude());
  82.  
  83. }
  84.  
  85. public String formatLocation2(Location location) {
  86. if (location == null)
  87. return "";
  88. return String.format(
  89. "%2$.4f",
  90. location.getLatitude(), location.getLongitude());
  91.  
  92. }
  93.  
  94. private void checkDisabled1() {
  95. dataGPS1 = "00,0000";
  96.  
  97. }
  98.  
  99. private void checkDisabled2() {
  100. dataGPS2 = "00,0000";
  101.  
  102. }
  103.  
  104. public void coordinatesGPS () {
  105.  
  106. String datGPS1 = dataGPS1;
  107.  
  108. System.out.println("Координаты =" + datGPS1);
  109. try {
  110. //----------------------------------------------
  111. client = new Socket("192.168.1.138"/*192.168.1.138"*/, 58000);
  112. //----------------------------------------------
  113.  
  114. DataOutputStream outData = new DataOutputStream(client.getOutputStream());
  115.  
  116. outData.writeUTF(datGPS1);
  117.  
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. 08-29 11:33:53.556 23612-23612/gpstracker I/System.out: Координаты =89,1706
  124. 08-29 11:33:53.556 23612-23612/gpstracker D/AndroidRuntime: Shutting down VM
  125. 08-29 11:33:53.556 23612-23612/gpstracker W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x418e4c08)
  126. 08-29 11:33:53.566 23612-23612/gpstracker E/AndroidRuntime: FATAL EXCEPTION: main
  127. Process: gpstracker, PID: 23612
  128. at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
  129. at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  130. at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  131. at libcore.io.IoBridge.connect(IoBridge.java:112)
  132. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  133. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  134. at java.net.Socket.startupSocket(Socket.java:567)
  135. at java.net.Socket.tryAllAddresses(Socket.java:128)
  136. at java.net.Socket.<init>(Socket.java:178)
  137. at java.net.Socket.<init>(Socket.java:150)
  138. at gpstracker.MainActivity.coordinatesGPS(MainActivity.java:137)
  139. at gpstracker.MainActivity$1.onLocationChanged(MainActivity.java:73)
  140. at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
  141. at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
  142. at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
  143. at android.os.Handler.dispatchMessage(Handler.java:102)
  144. at android.os.Looper.loop(Looper.java:146)
  145. at android.app.ActivityThread.main(ActivityThread.java:5603)
  146. at java.lang.reflect.Method.invokeNative(Native Method)
  147. at java.lang.reflect.Method.invoke(Method.java:515)
  148. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
  149. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
  150. at dalvik.system.NativeStart.main(Native Method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement