2012-04-16 76 views
0

對於android來說有些新鮮感,需要一些服務幫助。我有一個服務在間隔X輪詢當前位置。我想綁定到該服務並將getLastKnownLocation從服務傳遞到我的活動A.我不確定信息是如何從綁定服務傳遞給活動的,if它通過活頁夾或什麼。無論如何,這是我迄今爲止的代碼。如何從服務中獲取位置

服務:

public class LocationService extends Service implements LocationListener { 


    LocationManager myLocationManager; 
    public Location myLocation; 
    LocationListener myLocationListener; 
    public static final String TAG = LocationService.class.getSimpleName(); 
    MyDB db; 
    double latitude,longitude; 
    Cursor c; 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Log.d(TAG, "service started (onCreate)"); 
     db = new MyDB(getApplicationContext()); 
     myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     Criteria criteria = new Criteria(); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAccuracy(Criteria.ACCURACY_LOW); 
     String locationProvider = myLocationManager.getBestProvider(criteria, true); 
     myLocationManager.requestLocationUpdates(locationProvider, 1000*60*2, 100, this); 
     myLocation = myLocationManager.getLastKnownLocation(locationProvider); 


    } 
public class MyBinder extends Binder { 
      LocationService getService() { 
       return LocationService.this; 
      } 
     } 

活動答:

public class myActivity extends Activity { 
    LocationManager myLocationManager; 
    Location myLocation; 

    boolean isBound = false; 

    private LocationService mBoundService; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     bindLocationService(); 

} 
private void bindLocationService() { 
     try { 
      isBound = getApplicationContext().bindService(new Intent(getApplicationContext(), LocationService.class), mConnection, BIND_AUTO_CREATE); 
      bindService(new Intent(this, LocationService.class), mConnection, BIND_AUTO_CREATE); 
     } catch (SecurityException e) { 
      // TODO: handle exception 
     } 
    } 
private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 
      mBoundService = ((LocationService.MyBinder)service).getService(); 
      Log.d(LocationService.TAG, "activity bound to service"); 

     } 

     public void onServiceDisconnected(ComponentName className) { 

      mBoundService = null; 
      Log.d(LocationService.TAG, "activity unbound to service"); 
     } 
    }; 
} 
+0

要獲得一個位置,您只需要實現LocationListener並開始監聽,它在一個單獨的線程中執行yway。在這裏使用服務有什麼特別的理由嗎? – 2012-04-16 18:32:48

回答

3

從服務發送一個廣播這樣的:

Intent i = new Intent(NEW_MESSAGE); 
Bundle bundle = new Bundle(); 
bundle.putString("yourvalue", value); 
i.putExtras(bundle); 
sendBroadcast(i); 

並註冊接收您的活動是這樣的:

newMessage messageReceiver = new newMessage(); 
registerReceiver(messageReceiver, new IntentFilter(NEW_MESSAGE)); 

這是你在你活動類接收器:

public class newMessage extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    {  
     String action = intent.getAction(); 
     if(action.equalsIgnoreCase(IMService.NEW_MESSAGE)){  
     Bundle extra = intent.getExtras(); 
     String username = extra.getString("yourvalue"); 
    } 
} 
2

因爲當你想更新意圖的活動每一次服務和你的活動之間的溝通創建自定義BroadcastReceiver和服務廣播它這content new location info.like

Intent i = new Intent(); 
    i.setAction(CUSTOM_INTENT); 
    context.sendBroadcast(i); 

看到這個example定製廣播

0

你必須「訂閱」你的ActivityService它綁定。 Service可通過從Activity收到的Intent數據接收Messenger類型的對象。 所以你可以在你的ActivityMessenger裏面定義一個簡單的Handler。通過這種方式,Service可以將Message類型的對象發送到Activity,並將Message本身所需的所有信息發送出去。