Vorrei sapere come ricevere il GPS quando l'app Android è in background. Esiste un tutorial completo per spiegarlo?Esegui il listener GPS in background su Android
risposta
È possibile utilizzare un servizio che viene sempre eseguito in background e nel servizio in background è possibile ottenere latitudine e longitudine correnti e convertire ulteriormente lat e long in posizione corretta utilizzando la classe geocoder Android ... controlla il collegamento follwing:
la cosa che si farà in attività può essere fatto in servizio also..in l'onStart() of service ....
è necessario utilizzare Concetto di servizio anziché attività. http://developer.android.com/guide/components/services.html
Ecco un buon esempio: Background service needs to send GPS location on server
È necessario utilizzare un AlarmManager per attivare un intento attesa (tramite un ricevitore broadcast) per lanciare un servizio in background.
Alcuni codice di esempio per voi
Nella tua MainActivity
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notifyintent.setAction("android.intent.action.NOTIFY");
PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
notifysender);
classe AlarmReceiver
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context)
try {
lock = getLock(context);
lock.acquire();
context.startService(new Intent(context, UpdateCustomerRequests.class));
} finally {
if (lock.isHeld()) {
lock.release();
}
}
}
Il BroadcastReceiver
private static final String NAME = "com.commonsware.cwac.wakeful.WakefulIntentService";
private static volatile PowerManager.WakeLock lockStatic = null;
private static PowerManager.WakeLock lock;
// Needed since network will to work when device is sleeping.
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
}
Lo sfondo di servizio. Qui fino preavviso 2 punti importanti
- Wakelock, al fine di garantire che il dispositivo si sveglia e utilizza la rete in background
- Un hack per LocationManager al lavoro. Ho dovuto eseguire il debug di questo per circa 2 settimane, fidati di me, hai bisogno di questo. requestLocationUpdates e getLastKnownLocation semplicemente non ti danno la posizione quando vuoi. Da qui l'AlarmManager (che funziona molto meglio di un java TimerTask thingy).
Tutto questo funziona in un'app di produzione nel Play Store.
public class UpdateCustomerRequests extends IntentService implements LocationListener {
private static Context mainContext;
public UpdateCustomerRequests() {
super("UpdateCustomerRequests");
mHandler = new Handler();
me = this;
}
public static UpdateCustomerRequests getService() {
if (me == null)
me = new UpdateCustomerRequests();
return me;
}
@Override
final protected void onHandleIntent(Intent intent) {
mainContext = getApplicationContext();
Location myLocation;
if (HomeScreen.getLocationManager() != null) {
// this is needed to trigger a background location change. Since LocationManager does not work on Samsung phones. Its a hack needed.
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
myLocation = HomeScreen.getLocationManager().getLastKnownLocation(
LocationManager.NETWORK_PROVIDER);
if (myLocation != null)
onLocationChanged(myLocation);
else {
God.notifications.setSpeedNotification();
}
} else
Log.e("Taxeeta:PullPendingRequets", "Not activated");
}
@Override
public void onLocationChanged(final Location location) {
// Do your background stuff
}
}
Infine non dimenticare il tuo manifesto.
<service
android:name="com.taxeeta.UpdateCustomerRequests"
android:enabled="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" />
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
<receiver
android:name="com.taxeeta.HomeScreen$ResponseReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.taxeeta.intent.action.GET_SCREEN_UPDATES" />
</intent-filter>
</receiver>
Che cos'è la classe HomeScreen? – ChArAnJiT
- 1. Esegui processo in background in Android
- 2. Esegui Jupyter Notebook in Background su Docker
- 3. Come accendere il GPS su Android
- 4. esegui file bat in background
- 5. Apri/Esegui fotocamera dal servizio in background in Android
- 6. Android - Esegui in background - Servizio vs. classe Java Standard
- 7. gps orientamento in Android
- 8. Android su cassetto Listener chiuso
- 9. Esegui attività in background in JavaFX
- 10. Esegui attività in background in Javascript
- 11. android come smettere di gps
- 12. tracciamento GPS Android e Wakelock
- 13. Esegui processi in background con beanstalk elastico
- 14. Affidabile attività in background ripetuta su Android
- 15. Test del GPS in Android
- 16. Ottieni * posizione grossolana * dal provider GPS su Android
- 17. Ottieni coordinate GPS: Android
- 18. click listener su widget Android Pulsante
- 19. In che modo Nike + GPS su iPhone riceve gli aggiornamenti dell'accelerometro in background?
- 20. posizione corrente gps android
- 21. Il servizio in background deve inviare la posizione GPS sul server
- 22. Quanto è accurato il GPS Android?
- 23. Android: listener listbox
- 24. Animazione Android Listener
- 25. Come eseguire applicazioni Android in background
- 26. Impostare Listener OnClick su Action Bar Titolo in Android
- 27. Rileva listener su mese che scorre in times square android
- 28. Servizio di background su Android nativo react
- 29. Android Studio - Esegui apk firmato su emulatore
- 30. Android Enable e Disattiva GPS
Grazie, ma questo codice è per attività, come posso utilizzare il servizio. – Dev