问题
Somewhat new to android, need some help with services. I have a service which polls for current location at interval X. I want to bind to that service and pass getLastKnownLocation from the service to my Activity A. I am not sure exactly how the information is passed from the bound service to the activity, if its through the binder or what. Anyways, here is my code where I am at thus far.
Service:
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;
}
}
Activity 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");
}
};
}
回答1:
Send a Broadcast from your service like this:
Intent i = new Intent(NEW_MESSAGE);
Bundle bundle = new Bundle();
bundle.putString("yourvalue", value);
i.putExtras(bundle);
sendBroadcast(i);
and register for receiver in your activity like this:
newMessage messageReceiver = new newMessage();
registerReceiver(messageReceiver, new IntentFilter(NEW_MESSAGE));
This is your receiver in your activity class:
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:
For Communicate between Service and your Activity Create Custom BroadcastReceiver
and broadcast it from Service every time when you want to update your Activity with Intent which content new location info.like
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
see this example for custom Broadcast
回答3:
You have to "subscribe" your Activity
to the Service
it binds. The Service
can receive an object of type Messenger
via the Intent
data it receives from the Activity
.
So you can define a simple Handler
inside your Activity
and a Messenger
that uses it. This way the Service
can send objects of type Message
to the Activity
, shipping all the info you need inside the Message
itself.
来源:https://stackoverflow.com/questions/10179470/how-to-get-location-from-service