【问题标题】:How to get location from service如何从服务中获取位置
【发布时间】:2012-04-28 02:21:45
【问题描述】:

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

【问题讨论】:

  • 要获得一个位置,您只需要实现 LocationListener 并开始监听,无论如何它都是在一个单独的线程中执行的。你有什么特别的理由在这里使用服务吗?

标签: android service location


【解决方案1】:

像这样从您的服务发送广播:

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】:

    对于 Service 和您的 Activity 之间的通信创建自定义 BroadcastReceiver 并在每次您想使用 Intent 更新您的 Activity 时从 Service 广播它,其中包含新的位置信息。喜欢

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

    example 自定义广播

    【讨论】:

      【解决方案3】:

      您必须将您的Activity“订阅”到它绑定的ServiceService 可以通过从Activity 接收的Intent 数据接收Messenger 类型的对象。 所以你可以在你的Activity 中定义一个简单的Handler 和一个使用它的Messenger。这样Service 可以将Message 类型的对象发送到Activity,将您需要的所有信息发送到Message 本身。

      【讨论】:

        猜你喜欢
        • 2013-01-19
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多