Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testing.gps_location;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.Socket;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.nio.channels.DatagramChannel;
- import java.util.Date;
- import java.util.Enumeration;
- public class MainActivity extends AppCompatActivity {
- private Button b;
- private TextView t;
- private LocationManager locationManager;
- private LocationListener listener;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- t = (TextView) findViewById(R.id.textView);
- b = (Button) findViewById(R.id.button);
- locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- listener = new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- t.append("\n " + location.getLongitude() + " " + location.getLatitude());
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- }
- @Override
- public void onProviderEnabled(String s) {
- }
- @Override
- public void onProviderDisabled(String s) {
- Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivity(i);
- }
- };
- configure_button();
- //sendDataUDP("192.168.56.1");
- receiveDataTCP(5000,"192.168.56.1",t);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- switch (requestCode) {
- case 10:
- configure_button();
- break;
- default:
- break;
- }
- }
- void configure_button() {
- // first check for permissions
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
- , 10);
- }
- return;
- }
- // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
- b.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //noinspection MissingPermission
- locationManager.requestLocationUpdates("gps", 5000, 0, listener);
- }
- });
- }
- void sendDataUDP(String ipServer){
- final String finalIpServer = ipServer;
- new Thread(new Runnable(){
- // private final String SERVER_ADDRESS = ipServer;//public ip of my server
- private final static int SERVER_PORT = 10000;
- @Override
- public void run() {
- try {
- //Preparing the socket
- InetAddress serverAddr = InetAddress.getByName(finalIpServer);
- DatagramSocket socket = new DatagramSocket();
- //Preparing the packet
- byte[] buf = ("Hello Viet Anh").getBytes();
- DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVER_PORT);
- //Sending the packet
- Log.d("UDP", String.format("Sending: '%s' to %s:%s", new String(buf), finalIpServer, SERVER_PORT));
- socket.send(packet);
- Log.d("UDP", "Packet sent.");
- } catch (Exception e) {
- Log.e("UDP", "Client error", e);
- }
- }
- }).start();
- }
- void receiveDataTCP(int port, final String ipServer,final TextView textView){
- //t.append("\n Da vo roi nhe");
- final int Port = port;
- final String IpServer = ipServer;
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- t.append("\n Da vo roi nhe");
- Socket theSocket = new Socket(ipServer, Port);
- // textView.append("\n" +"Connected to "
- // + theSocket.getInetAddress() +" on port "
- // + theSocket.getPort() + " from port "
- // + theSocket.getLocalPort() + " of "
- // + theSocket.getLocalAddress());
- BufferedReader buff = new BufferedReader(new
- InputStreamReader(theSocket.getInputStream()));
- String inputLine;
- while ((inputLine = buff.readLine()) != null)
- {
- textView.append("\n" +inputLine);
- }
- theSocket.close();
- }
- catch (UnknownHostException e) {
- textView.append("\n" + e.toString());
- }
- catch (SocketException e) {
- textView.append("\n" + e.toString());
- }
- catch (IOException e) {
- textView.append("\n" + e.toString());
- }
- catch (Exception e) {
- textView.append("\n" + e.toString());
- }
- }
- }
- ).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement