如何从服务获得位置

时间:2012-04-16 18:21:05

标签: android service location

android有点新,需要一些服务帮助。我有一个服务,它以间隔X轮询当前位置。我想绑定到该服务并将getLastKnownLocation从服务传递给我的活动A.我不确定如何将信息从绑定服务传递到活动,如果它通过粘合剂或什么。无论如何,这是我到目前为止的代码。

服务:

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

活动A:

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

3 个答案:

答案 0 :(得分:4)

从您的服务发送广播,如下所示:

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

答案 1 :(得分:2)

用于在服务和您的活动之间进行通信创建自定义BroadcastReceiver并在每次要使用Intent更新您的活动时从服务广播该内容新位置信息。

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

请参阅此example了解自定义广播

答案 2 :(得分:0)

您必须将Activity“订阅”给它所绑定的ServiceService可以通过Messenger收到的Intent数据接收Activity类型的对象。 因此,您可以在Handler内定义简单的Activity,并使用Messenger定义Service。通过这种方式,Message可以将Activity类型的对象发送到Message,并在{{1}}内部提供您需要的所有信息。