gpt4 book ai didi

java - 我应该怎么做才能自动将我的 gps 坐标存储在 mysql 数据库中?

转载 作者:行者123 更新时间:2023-11-30 23:23:33 25 4
gpt4 key购买 nike

我的代码没有错误,我想学习的是在没有按钮的情况下将数据传递到我的数据库。目前我必须先点击我的按钮才能将我的 gps 坐标传递到我的数据库,但正如我所提到的,我想在我的应用程序运行时传递它。

这是我的代码:

**

public class Sample extends Activity{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

EditText textLat;
EditText textLong;

// url to create new product
private static String url_create_product = "http://10.0.2.2/SampleLocation/index.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
textLat = (EditText) findViewById(R.id.textLat);
textLong = (EditText) findViewById(R.id.textLong);
Button btnOK = (Button) findViewById(R.id.btnOK);
//to run the location class
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

//to run the createnewproduct class
btnOK.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});

}
class myLocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

class CreateNewProduct extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Sample.this);
pDialog.setMessage("Sending Location");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

/**
* Creating product
* */
protected String doInBackground(String... args) {
String latitude = textLong.getText().toString();
String longitude = textLat.getText().toString();

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("latitude", latitude));
params.add(new BasicNameValuePair("longitude", longitude));

// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);

// check log cat fro response
Log.d("Create Response", json.toString());

// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Echos.class);
startActivity(i);

// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}

**

最佳答案

假设您已经实现了数据库(例如通过扩展 SQLiteOpenHelper),那么您可以在监听器的 onLocationChanged() 方法中调用以写入数据库。

使用 locationManager 上的 requestLocationUpdates 调用中的参数仔细控制写入数据库的频率可能是最好的。如果您有一个已经写入数据库的按钮,那么这应该很简单。

关于java - 我应该怎么做才能自动将我的 gps 坐标存储在 mysql 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14435145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com