1
我試圖創建一個服務,該服務將在後臺運行並通知活動位置已更改/或用戶位於特定位置時。將位置對象從服務傳遞到活動中的處理程序
我仍然在學習Android編程,我有點不明白如何將Location對象傳遞給Activity。這裏是我的代碼如下所示:
public class MyActivity extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(MyActivity.this,LocationService.class));
}
...
Handler handler = new Handler() {
public void handleMessage(Message msg) {
Location loca=msg.obj; //I cannot do this,or can I??
float lat = (float) (loca.getLatitude());
}
};
}
public class LocationService extends Service {
...
public void updateLocation(Location loc) {
Message msg = Message.obtain();
msg.obj = loc;
messageHandler.sendMessage(msg);
}
}