2016-10-20 4 views
-1

Hallo bin neu für Android-Entwicklung, was mein Bedürfnis ist, Benutzer Standort für immer 15 Minuten holen und an den Server zu senden, habe ich gelernt, dass ich mit Service verwendet werden kann. Nach der Implementierung, wenn ich die App aus dem letzten Speicher löschen, höre ich auf zu aktualisieren Ich lese und versuchte alle Lösung Wie kann ich dies erreichen, auch wenn die letzte App gelöscht wird, um sicherzustellen, dass mein Dienst ausgeführt wird und holen Sie den Benutzer Standort lassen Sie mich meinen Dienstcode :Wie man einen GPS im Hintergrund Service für immer 15 Minuten mit Service laufen lässt?

public class GPSService extends Service 
{ 
    private static final String TAG = "BOOMBOOMTESTGPS"; 
    private LocationManager mLocationManager = null; 
    private static final int LOCATION_INTERVAL = 10000; 
    private static final float LOCATION_DISTANCE = 0; 

    private class LocationListener implements android.location.LocationListener 
    { 
     Location mLastLocation; 

     public LocationListener(String provider) 
     { 
      Log.e(TAG, "LocationListener " + provider); 
      mLastLocation = new Location(provider); 
     } 

     @Override 
     public void onLocationChanged(Location location) 
     { 
      Log.e(TAG, "onLocationChanged: " + location); 
      mLastLocation.set(location); 
     } 

     @Override 
     public void onProviderDisabled(String provider) 
     { 
      Log.e(TAG, "onProviderDisabled: " + provider); 
     } 

     @Override 
     public void onProviderEnabled(String provider) 
     { 
      Log.e(TAG, "onProviderEnabled: " + provider); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 
      Log.e(TAG, "onStatusChanged: " + provider); 
     } 
    } 

    LocationListener[] mLocationListeners = new LocationListener[] { 
      new LocationListener(LocationManager.GPS_PROVIDER), 
      new LocationListener(LocationManager.NETWORK_PROVIDER) 
    }; 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.e(TAG, "onStartCommand"); 
     super.onStartCommand(intent, flags, startId); 
     return START_STICKY; 
    } 

    @Override 
    public void onCreate() 
    { 
     Log.e(TAG, "onCreate"); 
     initializeLocationManager(); 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[1]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
     } 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[0]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
     } 
    } 
    @Override public void onTaskRemoved(Intent rootIntent){ 
       super.onTaskRemoved(rootIntent); 
     Intent restartService = new Intent(getApplicationContext(), 
       this.getClass()); 
     restartService.setPackage(getPackageName()); 
     PendingIntent restartServicePI = PendingIntent.getService(
       getApplicationContext(), 1, restartService, 
       PendingIntent.FLAG_ONE_SHOT); 
     AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI); 

    } 
    @Override 
    public void onDestroy() 
    { 
     Log.e(TAG, "onDestroy"); 
     super.onDestroy(); 
     Intent intent = new Intent("restartApps"); 
     sendBroadcast(intent); 
     if (mLocationManager != null) { 
      for (int i = 0; i < mLocationListeners.length; i++) { 
       try { 
        mLocationManager.removeUpdates(mLocationListeners[i]); 
       } catch (Exception ex) { 
        Log.i(TAG, "fail to remove location listners, ignore", ex); 
       } 
      } 
     } 
    } 

    private void initializeLocationManager() { 
     Log.e(TAG, "initializeLocationManager"); 
     if (mLocationManager == null) { 
      mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     } 
    } 
} 

Mein Rundfunkempfänger:

public class RestartService extends BroadcastReceiver { 

    private static final String TAG = "RestartService"; 

    public RestartService() { 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.e(TAG, "onReceive"); 
     context.startService(new Intent(context, GPSService.class)); 
    } 
} 

Mein Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="precisioninfomatics.backgroundgps"> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <application 
     android:name=".gspapp" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".GPS" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name=".MyLocationService" 
      android:exported="true" 
      android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE" 
      android:stopWithTask="false"> 
      <intent-filter> 
       <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" /> 
      </intent-filter> 
     </service> 
     <service 
      android:name=".GPSService" 
      android:enabled="true" 
      android:stopWithTask="false" 
      android:exported="false" /> 
     <receiver 
      android:name=".RestartService" 
      android:enabled="true" > 
      <intent-filter> 
       <action android:name="restartApps" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

Meine Probleme sind:

1.e Notwendigkeit Benutzer Lage auch nach dem klaren meinem letzten app 2. wenn der Benutzer Kraft Stopp in Einstellungen zu holen, wie mein Dienst

Vielen Dank im Voraus mit START_STICKY Service

Antwort

0

Start-Dienst neu starten, so dass Wenn es aufgrund einer Priorität abgebrochen wird, wird es neu erstellt, und wenn Sie diesen Dienst im Hintergrund und WAKE_Lock fortlaufend ausführen möchten und Alarm Manager verwenden, überprüfen Sie, ob es runnig ist oder nicht.

0

Sie können AlarmManager verwenden, um Ihren Service zu planen. Etwas wie:

AlarmManager alarm =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent checkLocationIntent = new Intent(context, CheckLocationService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, checkLocationIntent, 0); 

    alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_INTERVAL_MILLISECONDS, 
      ALARM_INTERVAL_MILLISECONDS, pendingIntent); 

Dies wird den CheckLocationService jede ALARM_INTERVAL_MILLISECONDS Zeit läuft

Verwandte Themen