Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从服务获取位置_Android_Service_Location - Fatal编程技术网

Android 如何从服务获取位置

Android 如何从服务获取位置,android,service,location,Android,Service,Location,对android有些陌生,需要一些服务方面的帮助。我有一个以间隔X轮询当前位置的服务。我想绑定到该服务,并将getLastKnownLocation从该服务传递到我的活动a。我不确定信息是如何从绑定服务传递到活动的,如果是通过活页夹还是什么。无论如何,这是我目前的代码 服务: public class LocationService extends Service implements LocationListener { LocationManager myLocationMana

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

要在服务和活动之间进行通信,请创建自定义的
广播接收器
,并在每次需要更新活动内容时从服务中广播该接收器。如

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

有关自定义广播

有关服务和您的活动之间的通信,请参见此内容创建自定义的
广播接收器
,并在每次您想要更新活动时从服务中广播它,其中包含新的位置信息。如

    Intent i = new Intent();
    i.setAction(CUSTOM_INTENT);
    context.sendBroadcast(i);
查看此自定义广播

您必须“订阅”您的
活动
到它绑定的
服务
服务
可以通过从
活动
接收的
意图
数据接收类型为
信使
的对象。 因此,您可以在
活动中定义一个简单的
处理程序
,以及使用它的
信使。通过这种方式,
服务
可以将类型为
消息
的对象发送到
活动
,在
消息
本身内部发送所需的所有信息。

您必须将
活动
订阅到它绑定的
服务。
服务
可以通过从
活动
接收的
意图
数据接收类型为
信使
的对象。
因此,您可以在
活动中定义一个简单的
处理程序
,以及使用它的
信使。通过这种方式,
服务
可以将类型为
消息
的对象发送到
活动
,在
消息
本身内部发送所需的所有信息。

通过服务发送广播,如下所示:

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));
并在您的活动中注册接收者,如下所示:

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

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

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));
并在您的活动中注册接收者,如下所示:

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

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