位置变化时调用一个类

时间:2022-12-01 23:25:48

I need guide in calling a class to run after my location changed. Can anyone help me please. I think the problem is because of this codes:

在我的位置发生变化后,我需要指导调用类来运行。任何人都可以帮助我。我认为问题是因为这个代码:

Intent i = new Intent(getApplicationContext(), CreateNewProduct.class);
startActivity(i);

This is the whole code:

这是整个代码:

(This is where I put some conditions when my location changes, also this is the part where I have to call the next class below.)

(这是我在我的位置发生变化时放置一些条件的地方,这也是我必须在下面调用下一个类的部分。)

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 to be called)

(要叫的课程)

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

3 个解决方案

#1


2  

You should execute aynctask as

你应该执行aynctask as

new CreateNewProduct().execute();

Because startActivity() is just for starting an activity as name suggests

因为startActivity()仅用于按名称建议启动活动

#2


2  

Class CreateNewProduct isn't Activity. You can't start its using Intent and startActivity() method. Best solution is create object of this class and call method from this class.

CreateNewProduct类不是Activity。您无法使用Intent和startActivity()方法启动它。最佳解决方案是创建此类的对象并从此类调用方法。

CODE SAMPLE :

代码示例:

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

            }
        }

I hope I helped. If you have more question, ask !

我希望我帮忙。如果您有更多问题,请询问!

#3


0  

Rather than calling class you can use LocationManager class to add a proximity for desired location.

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

you can implement it using addProximityAlert() method and PendingIntent.

您可以使用addProximityAlert()方法和PendingIntent来实现它。

Put the code that will be executed on the location change in PendingIntent.

将要执行的代码放在PendingIntent中的位置更改中。

Android Proximity Alert

Android Proximity Alert

#1


2  

You should execute aynctask as

你应该执行aynctask as

new CreateNewProduct().execute();

Because startActivity() is just for starting an activity as name suggests

因为startActivity()仅用于按名称建议启动活动

#2


2  

Class CreateNewProduct isn't Activity. You can't start its using Intent and startActivity() method. Best solution is create object of this class and call method from this class.

CreateNewProduct类不是Activity。您无法使用Intent和startActivity()方法启动它。最佳解决方案是创建此类的对象并从此类调用方法。

CODE SAMPLE :

代码示例:

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

            }
        }

I hope I helped. If you have more question, ask !

我希望我帮忙。如果您有更多问题,请询问!

#3


0  

Rather than calling class you can use LocationManager class to add a proximity for desired location.

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

you can implement it using addProximityAlert() method and PendingIntent.

您可以使用addProximityAlert()方法和PendingIntent来实现它。

Put the code that will be executed on the location change in PendingIntent.

将要执行的代码放在PendingIntent中的位置更改中。

Android Proximity Alert

Android Proximity Alert