【问题标题】:Periodic update of location details to server定期将位置详细信息更新到服务器
【发布时间】:2012-06-29 15:13:36
【问题描述】:

我是Stackoverflow 的新用户,这是我的第一个问题...

问题是……

如何获取位置信息的定期更新?我尝试过使用Service 类并在其中运行线程,但是,这只调用了一次。

谁能建议我做错了什么?谢谢你。 这是我的代码

   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

           }

     }
    }

我的更新答案 此调用服务类

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

【问题讨论】:

    标签: android android-service android-maps


    【解决方案1】:

    尝试在本课程的运行中执行您想做的事情, 该线程每 5 秒运行一次

    你可以随意修改。

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

    【讨论】:

    • Rinkalkumar 感谢您的建议,但我希望此应用程序在后台运行。所以我不能使用 Thread 类
    • @user1265782 您可以在服务的 onCreate 中使用此类,该服务将在后台运行。那应该工作。 :)
    • 服务类在后台运行是真的.. 但是获取位置详细信息的部分代码应该在特定的时间范围内执行,这就是问题所在
    • 试试现在我认为你可以做到。 :)
    猜你喜欢
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2018-11-25
    • 2014-08-06
    相关资源
    最近更新 更多