0
    public class LocationService extends Service {


        private Handler mHandler = new Handler();
        private Timer mTimer = null;
        private int mCount = 0;

        public static final long NOTIFY_INTERVAL = 10 * 1000;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            // cancel if already existed
            if (mTimer != null) {
                mTimer.cancel();
            } else {
                // recreate new
                mTimer = new Timer();
            }


            // schedule task
            mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);



        }

        @Override
        public void onDestroy() {
            mTimer.cancel();
        }

        private class TimeDisplayTimerTask extends TimerTask implements LocationListener {

            @Override
            public void run() {

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {

                        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0,
                                TimeDisplayTimerTask.this);

                    }

                });
            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if (location != null) {
                    Toast.makeText(getApplicationContext(), Double.toString(location.getLat(), Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), Double.toString(location.getLng(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Cant Get Location", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

        }

This code work fine on emulator. I can get location every 10 second. But when I test it on real phone, I don't see anything appear at all. Is it anything wrong with this code? Please show me how to fix this.

bon bon
  • 141
  • 1
  • 9

1 Answers1

0

Can you verify that your service is not being killed by the device on background?

(You can see this on Settings > Apps > Running tab > [Your app name] There should be a process count and service count there)

Android devices has it's own way if killing services on background. If that's the case, you should start your service on foreground. To prevent it from being killed.

How you ask? You should add this on start of your service:

Notification notification = new Notification();
startForeground(1, notification);

What is the notification you ask? This prompts the user when you're app is on background, you can substitute your toast with this. Here's a link related to its this. Check the accepted answer.

Android - implementing startForeground for a service?

Community
  • 1
  • 1
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94