3

I am new user to Stackoverflow and this is my first question...

Question is...

How to get the periodic updates of location details? I have tried with Service class and run thread inside it but,This is called only once.

Can any one suggest me what I am doing wrong? Thank you. Here is my code

   public class MyService extends Service 
{
String GPS_FILTER = "";
     Thread triggerService;
     LocationManager lm;
     GpsListener gpsLocationListener;
     boolean isRunning = true;
     @Override
     public void onCreate() 
     {
           // TODO Auto-generated method stub
           super.onCreate();
           Toast.makeText(getApplicationContext(), "Hello",Toast.LENGTH_LONG).show();
           GPS_FILTER = "MyGPSLocation";

     }

     @Override
     public void onStart(Intent intent, int startId) {
           // TODO Auto-generated method stub
           super.onStart(intent, startId);          
           triggerService = new Thread(new Runnable(){
                 public void run(){
                       try{
                             Looper.prepare();//Initialize the current thread as a looper.
                             lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                             Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
                             gpsLocationListener = new GpsListener();
                             long minTime = 30000; // 5 sec...
                             float minDistance = 10;
                             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
                                         minDistance, gpsLocationListener);
                             Looper.loop();
                       }catch(Exception ex){
                             System.out.println("Exception in triggerService Thread -- "+ex);
                       }
                 }
           }, "myLocationThread");
           triggerService.start();
     }

     @Override
     public void onDestroy() {
           // TODO Auto-generated method stub
           super.onDestroy();
           removeGpsListener();
     }

     @Override
     public IBinder onBind(Intent intent) {
           // TODO Auto-generated method stub
           return null;
     }

     private void removeGpsListener(){
           try{
                 lm.removeUpdates(gpsLocationListener);
           }
           catch(Exception ex){
                 System.out.println("Exception in GPSService --- "+ex);
           }
     }

     class GpsListener implements LocationListener{
           public void onLocationChanged(Location location) {
                 // TODO Auto-generated method stub
                 double latitude = location.getLatitude();
                 double longitude = location.getLongitude();
                 sendBroadcast(filterRes);
                 postdata();
           }**

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

           }

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

           }

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

           }

     }
    }

My updated answer This Call to service class

 Calendar cur_cal = Calendar.getInstance();
   cur_cal.setTimeInMillis(System.currentTimeMillis());
   Intent intent = new Intent(this, MyService.class);
   PendingIntent pintent = PendingIntent.getService(login.this, 0, intent, 0);
   AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


public class MyService extends Service 
{
     String GPS_FILTER = "";
     Thread triggerService;
     LocationListener locationListener;
     LocationManager lm;
     private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters
     private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000*60; // in Milliseconds
     protected LocationManager locationManager;
     boolean isRunning = true;
     @Override
     public void onCreate() 
     {
           // TODO Auto-generated method stub
           super.onCreate();
           GPS_FILTER = "MyGPSLocation";
//           locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//           locationManager.requestLocationUpdates(
//                   LocationManager.GPS_PROVIDER, 
//                   MINIMUM_TIME_BETWEEN_UPDATES, 
//                   MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
//                   new MyLocationListener());
     }

     @Override
     public void onStart(Intent intent, int startId)
     {
           // TODO Auto-generated method stub
           super.onStart(intent, startId);     
           turnGPSOn();
           Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
           LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
           locationListener = new MyLocationListener();
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, 1.0f, locationListener);

     }

     @Override
     public void onDestroy() {
           // TODO Auto-generated method stub
           super.onDestroy();
          // removeGpsListener();
     }

     @Override
     public IBinder onBind(Intent intent) {
           // TODO Auto-generated method stub
           return null;
     }

//     private void removeGpsListener(){
//           try{
//                 lm.removeUpdates(locationManager);
//           }
//           catch(Exception ex){
//                 System.out.println("Exception in GPSService --- "+ex);
//           }
//     }


     private class MyLocationListener implements LocationListener
     {

         public void onLocationChanged(Location location) 
         {
            postdata(location.getLatitude(),location.getLongitude());
             String message = String.format(
                     "New Location \n Longitude: %1$s \n Latitude: %2$s",
                     location.getLongitude(), location.getLatitude()
             );
             Toast.makeText(MyService.this, message, Toast.LENGTH_LONG).show();
             turnGPSOnOff();
         }

         public void onStatusChanged(String s, int i, Bundle b) {
//              Toast.makeText(MyService.this, "Provider status changed",
//                     Toast.LENGTH_LONG).show();
         }

         public void onProviderDisabled(String s) {
//             Toast.makeText(MyService.this,
//                     "Provider disabled by the user. GPS turned off",
//                     Toast.LENGTH_LONG).show();
         }

         public void onProviderEnabled(String s) {
//             Toast.makeText(MyService.this,
//                     "Provider enabled by the user. GPS turned on",
//                     Toast.LENGTH_LONG).show();
         }

     }
       alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 60*1000, pintent);
Nixit Patel
  • 4,435
  • 5
  • 30
  • 57
Sumit
  • 928
  • 2
  • 15
  • 34

1 Answers1

2

Try Puuting things that you want to do inside run of this class, This thread runs every 5 Seconds,

You can modify it as you want.

public class PeriodicChecker extends Thread
    {
        @Override
        public void run()
        {
            while(true) {
               System.out.println("Thread is doing something");
               //Put Your code here
               Thread.sleep(5000);
            }
        }

    }

    public OtherClass {
       public static void main(String args[]) {
          Thread t = new PeriodicChecker();
          t.start();
       }
    }
RPB
  • 16,006
  • 16
  • 55
  • 79
  • Rinkalkumar Thanks for suggestion but i want this application to run in background . So i cannot make use of Thread class – Sumit Jun 28 '12 at 08:24
  • @user1265782 You can use this class inside onCreate of your service which will be running in background itself. that should work. :) – RPB Jun 28 '12 at 08:25
  • Service class is running in background true.. but the part of code that get location details should execute in particular time frame that's the problem – Sumit Jun 28 '12 at 08:39
  • try Now i think you can do. :) – RPB Jun 29 '12 at 09:42