Meine App hat einen Intent-Dienst, der auf einem Webserver überprüft, ob "Start" oder "Stopp" Wort auf einer HTML-Seite alle 100000ms.Ort Listener in Android Intent-Service
Wenn es "Start" empfängt, startet es eine locationmanager.requestUpdateLocation(..)
, wenn es "Stopp" erhält, stoppt es mit locationmanager.removeUpdates(..)
.
Aber ich dieses Problem habe:
Als ich locationmanager.requestUpdateLocation(..)
rufe ich nie in onLocationChanged
meiner Zuhörer eingeben (das in der gleichen Leistungsumfang realisiert wird).
Dann beobachte ich, dass mein Intent-Dienst nicht ewig lebt :(Ich habe einen Empfänger, der den Moment erfasst, wenn Android bootet und startet es (und diese Arbeit) und einen Trick, in dem, wenn mein Intent-Dienst geschlossen ist durch Benutzer startet es selbst: P
Aber es lebt nie für immer, nach wenigen Stunden (oder vielleicht nur 30 Minuten) stirbt :(
Warum
Hier ist der Code:
@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();
}
}
}
Wie wäre es mit Alarms? https://developer.android.com/training/scheduling/alarms.html –