2011-09-12 12 views

Odpowiedz

0

Możesz skorzystać z usługi, która zawsze działa w tle .. a w usłudze tła możesz uzyskać bieżącą szerokość i długość geograficzną, a możesz dalej konwertować te liczby długie i długie w odpowiednie miejsce, używając klasy geograficznej Android ... sprawdzić następujący link:

http://www.codeproject.com/KB/android/GPSLocator.aspx

+2

Dzięki, ale ten kod dotyczy aktywności, w jaki sposób mogę korzystać z usługi. – Dev

0

rzecz które zrobisz w działalności można zrobić w służbie also..in na onStart() służby ....

4

Musisz użyć AlarmManager aktywować oczekującego intencję (za pośrednictwem odbiornika broadcast), aby uruchomić usługę tła.

Niektóre przykładowy kod dla ciebie

W swojej główną działalność

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); 

klasa 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(); 
     } 
    } 
} 

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); 
    } 
} 

usługa tła. Tutaj do zawiadomienia 2 ważne punkty

  1. blokada wybudzenia, aby upewnić się, że urządzenie budzi się i korzysta z sieci w tle
  2. hack dla LocationManager do pracy. Musiałem debugować to przez około 2 tygodnie, zaufaj mi, potrzebujesz tego. requestLocationUpdates i getLastKnownLocation po prostu nie dadzą ci lokalizacji, kiedy chcesz. Stąd AlarmManager (który działa znacznie lepiej niż Java TimerTask).

Wszystko to działa w aplikacji produkcyjnej w sklepie z zabawkami.

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 
} 

} 

Na koniec nie zapominaj o swoim manifeście.

<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> 
+0

Co to jest klasa HomeScreen? – ChArAnJiT

Powiązane problemy