Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中的服务中调用位置管理器两次_Java_Android - Fatal编程技术网

Java 在android中的服务中调用位置管理器两次

Java 在android中的服务中调用位置管理器两次,java,android,Java,Android,在我的服务中,我尝试给位置经理打两次电话 首先,它会在启动时调用Manager,然后我会设置一个计时器,持续20秒,然后我会再次调用它 第一次工作,但第二次20秒后,位置管理器根本没有被调用 下面是我尝试过的代码: public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.e("Location1", "Inside onStart");

在我的服务中,我尝试给位置经理打两次电话

首先,它会在启动时调用Manager,然后我会设置一个计时器,持续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) {

    }

}   

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

我认为您在
onStart
方法中错过了对
timer.start()
的调用