Android Intent Service中的位置监听器

时间:2017-03-31 18:12:49

标签: android location locationmanager locationlistener

我的应用程序有一个Intent服务,如果有" start"它会继续检查Web服务器。或者"停止"每100000毫秒在一个html页面上的字。

如果收到"开始"它开始locationmanager.requestUpdateLocation(..),如果收到"停止"它会以locationmanager.removeUpdates(..)停止。

但是我遇到了这个问题:

当我致电locationmanager.requestUpdateLocation(..)时,我从不进入我的听众onLocationChanged(在同一服务范围内实施)。

然后,我观察到我的Intent服务并没有永远存在:(我把一个接收器捕获Android启动并启动它(和这项工作)的时刻以及当我的Intent服务是用户关闭它重启自己:P

但它永远不会永远存在,经过几个小时(或者可能只有30分钟)它就会死去:(

为什么呢?

以下是代码:

@Override
protected void onHandleIntent(@Nullable Intent intent) {

  Log.i("Service", "Started");

  LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
  LocationListener  locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
    //IT NEVER ENTER HERE 
    Log.i("LOC", "LAT: " + location.getLatitude() +" LONG: "+location.getLongitude());
    }

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

    public void onProviderEnabled(String provider) {
    }

    public void onProviderDisabled(String provider) {
    //NEVER ENTER ALSO HERE
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    }
  };

  int localizzazioneAttiva=0;
  int delay = 0;

  while (true) {
    Log.i("SERVICE_CheckSito", "Check Sito STARTED");

    String inputLine = "";
    StringBuffer stringBuffer = new StringBuffer();
    //Check Permission and SKD_INT
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED)
    {
      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {

        URL url;
        try
        {
          url = new URL("http://www.sito.com/file.html");
          HttpURLConnection httpURLConnection;
          httpURLConnection = (HttpURLConnection) url.openConnection();
          httpURLConnection.connect();
          if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

            try {
              while ((inputLine = bufferedReader.readLine()) != null) {
                stringBuffer.append(inputLine).append("\n");
              }
            } finally {
              bufferedReader.close();
            }
          }
        } catch (IOException e) {
        //  e.printStackTrace();
        }
      } else return;
    }

    if (stringBuffer.toString().equals("start\n") && 0==localizzazioneAttiva )
    {
      localizzazioneAttiva=1;
      delay = 1000;
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 0, locationListener);
    }
    if(stringBuffer.toString().equals("stop\n")) {
      delay = 2000;
      locationManager.removeUpdates(locationListener);
      localizzazioneAttiva=0;
    }

    Log.i("SERVICE_CheckSito", "Message from server: " + stringBuffer.toString());
    Log.i("SERVICE_CheckSito", "Check Sito FINISHED");
    try {
      Thread.sleep(delay);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

3瓶啤酒后我不应该回答。我的坏,没看到你追加/ n。

考虑使用普通服务和FusedLocationProvider。