安卓Service向Activity传递数据,更新UI

本文介绍了如何在Android中通过Service执行定时任务和网络请求获取位置信息,并利用接口回调、Handler及活动和服务绑定的方式,将数据传递到Activity,实现在地图上展示定位结果。关键步骤包括在Service中设置回调接口,通过LocationBinder获取Service实例,以及在Activity中接收并更新UI的消息处理。
摘要由CSDN通过智能技术生成

接口回调、Handler、活动和服务绑定

1服务:执行定时任务,发起网络请求定位,请求到的结果传递到活动,在地图上展示。

2活动关键代码:
绑定服务后会获取LocationService.LocationBinder对象,在此处调用getLocationService()方法获取Service实例,service设置回调接口获取位置信息,并发送消息,handler接收消息后更新UI。
//绑定服务

 private LocationService  service;
    private ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            service=((LocationService.LocationBinder)iBinder).getLocationService();
            service.setDataCallback(new LocationService.DataCallback() {
                @Override
                public void updateui(LocationInfo locationInfo) {
                    Message message=new Message();
                    message.what=LocationService.UpdateLocation;
                    message.obj=locationInfo;
                    handler.sendMessage(message);

                }
            }); 
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

//handler处理消息更新UI
 public  Handler handler=new Handler(){
        public void handleMessage(Message message){
            switch (message.what){
                case LocationService.UpdateLocation:
                    //更新UI
                    sLocationInfo=(LocationInfo)message.obj;
                    baiduMapUtil.showAtLocation(sLocationInfo);
                    baiduMapUtil.mBaiduMap.clear();
                    baiduMapUtil.addOverLay(sLocationInfo);
                    baiduMapUtil.GeoCode(sLocationInfo);
                    baiduMapUtil.setGeoCallback(new BaiduMapUtil.Geocallback() {
                        @Override
                        public void geo(String address) {
                            sLocationInfo.setAddress(address);
                            baiduMapUtil.showInfoWindow(sLocationInfo);
                        }

                        @Override
                        public void Poi(List<HospitalInfo> list) {

                        }
                    });
                    break;
            }
        }
    };



public class LocationService extends Service {
   
    public static final int UpdateLocation=1;//更新UI
    private LocalDB localDB;
    private List<People> peoples;
    private People mCurrentPeople;
    private String mCurrentUserId;
    public LocationInfo locationInfo;
    private DataCallback dataCallback=null;
    public LocationService() {
    }
    private LocationBinder mBinder=new LocationBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        localDB=new LocalDB(this);
        peoples=localDB.getMemberList();
        mCurrentPeople=peoples.get(0);
        mCurrentUserId=mCurrentPeople.getUserid();
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
    //自定义的异步定时任务从网络请求定位,获取经纬度,没隔10秒请求一次
    new MyTask().execute(mCurrentUserId,"0",new Date().getTime()+"");
     AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE);
     int anHour=60*60*1000;
     long triggerAtTime= SystemClock.elapsedRealtime()+10*1000;
     Intent i=new Intent(this, LocationReceiver.class);
     PendingIntent                pendingIntent=PendingIntent.getBroadcast(this,0,i,0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pendingIntent);
    return super.onStartCommand(intent,flags,startId);
    }
   //定义Binder,获取服务实例
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值