0
我在我的android應用程序上使用LOcation服務,並且已將'location.getLatitude()'和'location.getLongitude()'放置到位。運行後,座標顯示在屏幕上。 我的擔心是,我需要將座標發送到我的本地主機(即使它們被自動刷新)。下面 是我picklocation.java樣品Android位置服務,如何發送位置到MySQL位置
package com.example.vic.cdmes_;
import android.Manifest;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class pickLocation extends AppCompatActivity {
Button buttonLocation;
TextView viewCoordinates;
private LocationListener locationListener;
private LocationManager locationManager;
Button button;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_location);
//portion for the date picker
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
ShowDialogForDateOnClick();
//portion for the gps picker
buttonLocation = (Button) findViewById(R.id.locateme);
viewCoordinates = (TextView)findViewById(R.id.viewCoordinates);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {//called whenever the location is updated
viewCoordinates.append("\n "+location.getLatitude()+ " "+location.getLongitude());//displays the value of latitude and longitude
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {//called to enable user put GPS on, will take the user to settings panel, if not.
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
},10);
return;
}
}
else {
configureButton();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case 10:
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
configureButton();
return;
}
}
private void configureButton() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
}
});
}
public void ShowDialogForDateOnClick()
{
button= (Button)findViewById(R.id.pickdate);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id)
{
if(id== DIALOG_ID)
return new DatePickerDialog(this, dpickerListener,year_x,month_x,day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dpickerListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
year_x=year;
month_x=monthOfYear + 1;
day_x=dayOfMonth;
Toast.makeText(pickLocation.this,day_x +"/"+month_x+"/"+year_x, Toast.LENGTH_LONG).show();
}
};
}
如何將我發送以下到我的數據庫?
public void onLocationChanged(Location location) {//called whenever the location is updated
viewCoordinates.append("\n "+location.getLatitude()+ " "+location.getLongitude());//displays the value of latitude and longitude
}
你是指什麼樣的數據庫? – Vyacheslav
它可以通過REST API完成。 – DkThakur
@Vyacheslav,MySql數據庫 – VicMo