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

标签 java android mysql gps

我的代码没有错误,我想学习的是在没有按钮的情况下将数据传递到我的数据库。目前我必须先点击我的按钮才能将我的 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/

相关文章:

java - return 语句后关闭结果集

android - 如何在 Android AIR 应用程序的 native 扩展中使用 GooglePlayServices 库?

MySQL 4.1 到 5.1 复制 : "ON DUPLICATE KEY UPDATE" and "ON UPDATE CURRENT_TIMESTAMP" not working

php - Mysql 错误 #1305 FUNCTION db.sys_exec 不存在

mysql - Google Cloud SQL Performance Tiers 与 Amazon AWS RDS for MySQL 相比如何?

java - 在Java中,如果类的父/同级对象没有通过扩展关联,我如何引用它们?

java - 为什么Eclipse内存分析器不显示InetSocketAddress字符串值?

android - 在 android 中帮助编辑文本

java - 将多个对象写入和读取到文件

java - gitlab-CI 不使用 maven 和 java 10 构建?