-3

I have this code, which shows me the ** latitude ** and ** longitude ** of the user in a Toast in a time interval, but I need to save this information inMySQL every time the system updates the location . I'm really needing this code to complete a job.

MainActivity.java

    package com.example.quantide.gps_teste;
    import android.Manifest;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.provider.Settings;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity implements LocationListener {

    private LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 1, this);

    }

    @Override
    public void onLocationChanged(Location location) {

        String msg = "New Latitude: " + location.getLatitude()
                + "New Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {

        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "Gps is turned off!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(getBaseContext(), "Gps is turned on!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

upload.php

  <?php

$latitude = $_POST['lati'];
$longitude= $_POST['longi'];

$up = mysql_query("UPDATE gps SET lati = $latitude, longi = $longitude");

?>
  • 1
    Welcome to Stack Overflow. We are not here to do your work for you, we are here to help. Please post the code you have tried in PHP and we will try and point you in the right direction. MySQL Workbench can give you the MySQL commands needed for PHP. You'd then just need to POST the values and use them. – apmartin1991 Dec 02 '16 at 15:37
  • Okay, I added my php code, as you suggested. @apmartin1991 – Bonfim Junior Dec 02 '16 at 15:50
  • My biggest difficulty is in the java part. @apmartin1991 – Bonfim Junior Dec 02 '16 at 15:56
  • post the android code which sends it to the server – apmartin1991 Dec 02 '16 at 15:56
  • This is the question, I have no idea how to do this part of the code, I have tried something but without success. @apmartin1991 – Bonfim Junior Dec 02 '16 at 16:00
  • 1
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Dec 02 '16 at 16:38

1 Answers1

0

Create a backend and use your android app to post the values to it. Then, using MySQL commands, you can save it to a database.

UPDATE

Click here for a step-by-step walkthrough of how to create a backend. You will also find information about how you can post your data to the server. If you need some extra reference, you can visit this site. Then, you can visit this site for a tutorial on how to use your mySQL database directly with java.

Feel free to ask if you need anything else. If you're satisfied with the answer, mark it as accepted. Hope I helped :D

Ishan Manchanda
  • 459
  • 4
  • 19
  • I need some code examples of how I can do that sent from the co-signed to my web server. This is the question, I have no idea how to do this part of the code, I have tried something but without success. @Rippr – Bonfim Junior Dec 02 '16 at 16:02