1

我试图在我的服务中两次致电位置经理。

首先它将调用 Manager 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) {

    }

}   
4

2 回答 2

0

在您的情况下,位置更新的最小距离可能是 10 米。

于 2013-04-26T10:18:17.697 回答
0

我认为您在方法中缺少timer.start()调用onStart

于 2013-04-26T12:16:33.220 回答