Advertisement
vietanhlehuu

Android Update

Oct 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 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.provider.Settings;
  13. import android.support.annotation.NonNull;
  14. import android.support.annotation.Nullable;
  15. import android.support.v4.app.ActivityCompat;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.DataInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.net.DatagramPacket;
  27. import java.net.DatagramSocket;
  28. import java.net.InetAddress;
  29. import java.net.NetworkInterface;
  30. import java.net.Socket;
  31. import java.net.SocketException;
  32. import java.net.UnknownHostException;
  33. import java.nio.channels.DatagramChannel;
  34. import java.util.Date;
  35. import java.util.Enumeration;
  36.  
  37. public class MainActivity extends AppCompatActivity {
  38.  
  39. private Button b;
  40. private TextView t;
  41. private LocationManager locationManager;
  42. private LocationListener listener;
  43.  
  44.  
  45. @Override
  46. protected void onCreate(@Nullable Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48.  
  49. setContentView(R.layout.activity_main);
  50.  
  51. t = (TextView) findViewById(R.id.textView);
  52. b = (Button) findViewById(R.id.button);
  53.  
  54. locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  55.  
  56.  
  57. listener = new LocationListener() {
  58. @Override
  59. public void onLocationChanged(Location location) {
  60. t.append("\n " + location.getLongitude() + " " + location.getLatitude());
  61. }
  62.  
  63. @Override
  64. public void onStatusChanged(String s, int i, Bundle bundle) {
  65.  
  66. }
  67.  
  68. @Override
  69. public void onProviderEnabled(String s) {
  70.  
  71. }
  72.  
  73. @Override
  74. public void onProviderDisabled(String s) {
  75.  
  76. Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  77. startActivity(i);
  78. }
  79. };
  80.  
  81. configure_button();
  82. //sendDataUDP("192.168.56.1");
  83. receiveDataTCP(5000,"192.168.56.1",t);
  84. }
  85.  
  86. @Override
  87. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  88. switch (requestCode) {
  89. case 10:
  90. configure_button();
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96.  
  97. void configure_button() {
  98. // first check for permissions
  99. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  100. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  101. requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
  102. , 10);
  103. }
  104. return;
  105. }
  106. // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
  107. b.setOnClickListener(new View.OnClickListener() {
  108. @Override
  109. public void onClick(View view) {
  110. //noinspection MissingPermission
  111. locationManager.requestLocationUpdates("gps", 5000, 0, listener);
  112. }
  113. });
  114. }
  115.  
  116. void sendDataUDP(String ipServer){
  117. final String finalIpServer = ipServer;
  118. new Thread(new Runnable(){
  119. // private final String SERVER_ADDRESS = ipServer;//public ip of my server
  120. private final static int SERVER_PORT = 10000;
  121. @Override
  122. public void run() {
  123. try {
  124. //Preparing the socket
  125. InetAddress serverAddr = InetAddress.getByName(finalIpServer);
  126. DatagramSocket socket = new DatagramSocket();
  127. //Preparing the packet
  128. byte[] buf = ("Hello Viet Anh").getBytes();
  129. DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVER_PORT);
  130.  
  131. //Sending the packet
  132. Log.d("UDP", String.format("Sending: '%s' to %s:%s", new String(buf), finalIpServer, SERVER_PORT));
  133. socket.send(packet);
  134. Log.d("UDP", "Packet sent.");
  135. } catch (Exception e) {
  136. Log.e("UDP", "Client error", e);
  137. }
  138. }
  139. }).start();
  140. }
  141. void receiveDataTCP(int port, final String ipServer,final TextView textView){
  142. //t.append("\n Da vo roi nhe");
  143. final int Port = port;
  144. final String IpServer = ipServer;
  145. new Thread(new Runnable() {
  146. @Override
  147. public void run() {
  148. try {
  149. t.append("\n Da vo roi nhe");
  150.  
  151. Socket theSocket = new Socket(ipServer, Port);
  152. // textView.append("\n" +"Connected to "
  153. // + theSocket.getInetAddress() +" on port "
  154. // + theSocket.getPort() + " from port "
  155. // + theSocket.getLocalPort() + " of "
  156. // + theSocket.getLocalAddress());
  157. BufferedReader buff = new BufferedReader(new
  158. InputStreamReader(theSocket.getInputStream()));
  159. String inputLine;
  160. while ((inputLine = buff.readLine()) != null)
  161. {
  162. textView.append("\n" +inputLine);
  163. }
  164.  
  165. theSocket.close();
  166.  
  167.  
  168.  
  169. }
  170. catch (UnknownHostException e) {
  171.  
  172. textView.append("\n" + e.toString());
  173. }
  174. catch (SocketException e) {
  175. textView.append("\n" + e.toString());
  176. }
  177. catch (IOException e) {
  178. textView.append("\n" + e.toString());
  179. }
  180. catch (Exception e) {
  181. textView.append("\n" + e.toString());
  182. }
  183. }
  184. }
  185. ).start();
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement