GPS трекер для Android - PullRequest
       8

GPS трекер для Android

0 голосов
/ 18 мая 2018

Я пытался создать поток, способный постоянно отправлять координаты GPS в мою БД.Это мой код:

public class trackThread extends Thread {
    Context mContext;
    public trackThread(Context mContext) {
        this.mContext = mContext;
    }

    @SuppressLint("MissingPermission")
    public void run() {
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        trackThread.MyCurrentLoctionListener locationListener = new trackThread.MyCurrentLoctionListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }



    public class MyCurrentLoctionListener implements android.location.LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            final String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();



            // Instantiate the RequestQueue.
            RequestQueue queue = Volley.newRequestQueue(mContext);
            String url ="http://example.org/send.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude();

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Display the first 500 characters of the response string.


                            Log.e("Response is: "+ response.toString(), myLocation);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("That didn't work!", myLocation);
                }
            });

            // Add the request to the RequestQueue.
            queue.add(stringRequest);
            Log.e("La risposta è ", stringRequest.toString());


        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }

}

Этот поток вызывается в этом методе, находящемся в классе Java:

public void inviaGps(View v) {
    if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||  ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.

        Toast t = Toast.makeText(this, "Devi abilitare i permessi", Toast.LENGTH_SHORT);
        t.show();
        return;
    }
    trackThread p = new trackThread(getApplicationContext());
    new Thread(p).start();
}

этот метод вызывается нажатием на кнопку простого иобычная деятельность.

Я думаю, что проблема вызвана неправильным использованием контекста (извините, я не очень разбираюсь в кодировании Android).PS: Я также попытался добавить простой Log.e ниже locationManager.requestLocationUpdates() для отладки и попытаться понять проблему.Это было показано мне

...