我的代码没有错误,我想学的是没有按钮就将数据传递到我的数据库。目前我必须首先点击我的按钮才能将我的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();
}
}
}
**
答案 0 :(得分:0)
假设您已经实现了数据库(例如通过扩展SQLiteOpenHelper),那么您可以调用在侦听器的onLocationChanged()方法中写入数据库。
可能最好使用locationManager上对requestLocationUpdates的调用中的参数来仔细控制您写入数据库的频率。如果你有一个已经写入数据库的按钮,那么这应该很简单。