2013-04-26 49 views
1

我正尝试在我的服务中调用位置管理器两次。在android中的服务中调用位置管理器两次

首先,它会打电话给经理onStart,然后我把一个计时器20秒,我再次调用它。

第一次它是工作,但20秒后的位置管理器在全称为第二次..

下面是我曾尝试代码:

 public void onStart(Intent intent, int startId) 
    { 
    super.onStart(intent, startId); 

    Log.e("Location1", "Inside onStart"); 

    t = new Timer(); 

    locationFirst = "First"; 

    talker = new TextToSpeech(this, this); 

    location = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


    locationListener = new MyLocationListener(); 
    location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10,locationListener); 
} 

      public void testMethod() 
     { 
    //Declare the timer 


     //Set the schedule function and rate 
       t.scheduleAtFixedRate(new TimerTask() { 

     @Override 
     public void run() { 

      Log.e("Time",""+time); 
      time += 1; 
      String todis = String.valueOf(time); 


      if(todis.contains("20")) 
      { 
       Looper.myLooper(); 
       Looper.prepare(); 

       talker.speak("Testing Service inside 20 in a App",TextToSpeech.QUEUE_ADD,null); 
       locationFirst = "Second"; 
       locationTocall(); 
       time = 0; 
       t.cancel(); 

      } 

     } 

       }, 0, 1000); 


      } 


    public void locationTocall() 
{ 
    locationListener = new MyLocationListener(); 
    location.requestLocationUpdates(location.NETWORK_PROVIDER, 5000, 10,locationListener); 
      } 


    private class MyLocationListener implements LocationListener 
{ 
    @Override 
    public void onLocationChanged(Location loc) 
    { 
     Log.e("Location1", "Inside MyLocation"); 

     String longitude = String.valueOf(loc.getLongitude()); 
     String latitude = String.valueOf(loc.getLatitude()); 

     String s = latitude+"\n"+longitude; 

     Log.e ("Location1", " "+s); 

     location.removeUpdates(locationListener); 
        if(locationFirst.equalsIgnoreCase("FIRST")) 
     {   
      testMethod(); 
     } 
     else 
     { 

     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

} 

回答

0

分钟的距离在你的情况下位置更新是10米 也许正是因为如此。

0

我想你在onStart方法中缺少对timer.start()的调用。

相关问题