Java 位置更改时调用类

Java 位置更改时调用类,java,android,Java,Android,我需要向导在我的位置改变后调用一个类来运行。谁能帮帮我吗。我认为问题在于这些代码: Intent i = new Intent(getApplicationContext(), CreateNewProduct.class); startActivity(i); 这是全部代码: 这是当我的位置改变时,我放置一些条件的地方,也是我必须调用下面的下一个类的地方 class myLocationlistener implements LocationListener { @Overr

我需要向导在我的位置改变后调用一个类来运行。谁能帮帮我吗。我认为问题在于这些代码:

Intent i = new Intent(getApplicationContext(), CreateNewProduct.class);
startActivity(i);
这是全部代码:

这是当我的位置改变时,我放置一些条件的地方,也是我必须调用下面的下一个类的地方

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));

                Intent i = new Intent(getApplicationContext(), CreateNewProduct.class);
                startActivity(i);
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
要调用的类

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) {
                    Toast.makeText(getBaseContext(), "Success",
                            Toast.LENGTH_SHORT).show();
                } 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();
        }
    }

您应该按照以下方式执行aynctask:

new CreateNewProduct().execute();

因为startActivity只是用于启动一个活动,正如名称所示,

类CreateNewProduct不是活动。您无法使用Intent和startActivity方法启动它。最好的解决方案是创建这个类的对象并从这个类调用方法

代码示例:

@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));

               CreateNewProduct p = new CreateNewProduct();
               p.execute();

            }
        }

我希望我能帮忙。如果你有更多的问题,问吧

您可以使用LocationManager类为所需位置添加接近度,而不是调用类

您可以使用addProximityAlert方法和PendingEvent实现它

将要对位置更改执行的代码放入PendingEvent


startActivity,顾名思义,启动一个活动,而不是一个AsyncTask。那么我应该如何用AsyncTask调用我的类呢?谢谢你,我写了你的答案…请标记其中一个答案为已接受。谢谢