Andrew Barker
Andrew Barker

Reputation: 73

Make on click button action into service

I have a button that when clicked shows your location, compares it to an exact fixed location, and says how many meters are between you and that location, What i want to do is make the action when clicked into a service so it runs as the application starts and keeps running until the application closes. Here is my button code

                @Override
            public void onClick(View arg0) {
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);
                TextView status = (TextView) findViewById(R.id.status);
                TextView where = (TextView) findViewById(R.id.where);
                TextView greet = (TextView) findViewById(R.id.greet);

                // check if GPS enabled
                if (gps.canGetLocation()) {
                    double datapostLat = -26.106589;
                    double datapostLong = 28.015150;
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                    // \n is for new line
                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    status.setText("YOUR LOCATION" + "\n" + "Latitude: " + latitude + "\n" + "Longitude: " + longitude);
                    try {
                        float[] results = new float[1];
                        Location.distanceBetween(latitude, longitude, datapostLat, datapostLong, results);
                        System.out.println(results[0]);
                        where.setText("You are: " + results[0] + " M" + " Away from Datapost");
//                        Toast.makeText(AndroidGPSTrackingActivity.this, ""+ results[0] +"", Toast.LENGTH_SHORT).show();
                        if(results[0]<50) {
                            greet.setText("Welcome to Datapost");
                        } else {
                            greet.setText("You are leaving Datapost");
                        }
                    } catch (Exception e) {
                        e.getStackTrace();
                        //for debugging
                        Toast.makeText(AndroidGPSTrackingActivity.this, "error: " + "\n" + e, Toast.LENGTH_SHORT).show();
                    }

                } else {
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                }


            }

In short I need to make a simple service in place of this button, is it possible?

Upvotes: 0

Views: 42

Answers (2)

JohnWatsonDev
JohnWatsonDev

Reputation: 1247

Buddy~

Your best choice is IntentService which help you off-load operations onto a separate thread running in the background.

You could ceaseless get the distance between the fixed location and the newest location from LocationProvider. Then you could continuously send the result to your Activity through LocalBroadcastManagerand update your UI in Main(UI) Thread.

Reference: https://developer.android.com/training/run-background-service/index.html

Upvotes: 1

Tushar Saha
Tushar Saha

Reputation: 2106

I assume u want to track location continuously so this should help.

https://developer.android.com/training/location/receive-location-updates.html

Upvotes: 0

Related Questions