J'ai trouvé un tutoriel sur le service de localisation GPS. Tout fonctionne bien. La prochaine chose que je veux écrire est de passer des coordonnées de l'application à mysql. J'ai essayé plusieurs tutoriel sur mysql Android mais je ne sais pas comment les appliquer à ce code. Ce que j'essaye de réaliser est de onLocationChanged envoyer la latitude et la longitude à la base de données.Insérer des coordonnées à mysql depuis android
Quelqu'un pourrait-il me dire comment faire?
Voici le code:
connect.php
<?php
define('HOST','localhost');
define('USER','');
define('PASS','');
define('DB','coordinates');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
insert.php
<?php
require_once('connect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
$la = $_POST['la_string'];
$lo = $_POST['lo_string'];
$get_result = $con->query("INSERT INTO gps(la,lo) VALUES ('$la','$lo')");
if($get_result === true){
echo "Successfully Registered";
}else{
echo "Not Registered";
}
}else{
echo 'error';
}
>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button start, stop, exit;
private TextView data_lo, text_lo, text_la, data_la;
private BroadcastReceiver broadcastReceiver;
private String la_string,lo_string;
@Override
protected void onResume() {
super.onResume();
if (broadcastReceiver == null) {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
data_la.setText("" + intent.getExtras().get("latitude"));
data_lo.setText("" + intent.getExtras().get("longitude"));
}
};
}
registerReceiver(broadcastReceiver, new IntentFilter("location_update"));
}
@Override
protected void onDestroy() {
super.onDestroy();
if(broadcastReceiver != null){
unregisterReceiver(broadcastReceiver);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
data_la = (TextView) findViewById(R.id.data_la);
data_lo = (TextView) findViewById(R.id.data_lo);
text_la = (TextView) findViewById(R.id.text_la);
text_lo = (TextView) findViewById(R.id.text_lo);
exit = (Button) findViewById(R.id.exit);
if(!runtime_permissions())
enable_buttons();
}
private void enable_buttons() {
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i =new Intent(getApplicationContext(),GPS_Service.class);
startService(i);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {Intent i = new Intent(getApplicationContext(),GPS_Service.class);
stopService(i);
}
});
}
private boolean runtime_permissions() {
if(Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
enable_buttons();
}else {
runtime_permissions();
}
}
}}
GPS_Service.java
public class GPS_Service extends Service {
private LocationListener locationListener;
private LocationManager locationManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
i.putExtra("latitude", location.getLatitude());
i.putExtra("longitude", location.getLongitude());
sendBroadcast(i);
}
@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);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,locationListener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(locationListener);
}
}}
Je ne comprends toujours pas, où dois-je mettre ce code. Dans MainActivity ou dans GPS_Service –
cela dépend de quand vous voulez appeler le serveur pour stocker les valeurs – fmaccaroni
Dans l'application j'ai deux boutons, démarrer et arrêter. Je clique sur Démarrer, attend les coordonnées. L'heure de mise à jour est toutes les 3 secondes. Je voudrais stocker des valeurs automatiquement chaque fois que l'emplacement est changé. –