android另起线程通过网络获取所在地

通过网络获取所在地这个动作可能在wifi连接信号不好的时候比较慢,这个时候需要另外新启一个线程来做这件事情,同时LocationManager中的requestLocationUpdates函数需要给新起的进程发送消息,所以新起的进程必须有消息循环。

 

    /*get the city name*/
    public String getCityName(){
        if (cityName != null)
            return cityName;
       
        locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
          LocationProvider provider = locMan.getProvider(LocationManager.NETWORK_PROVIDER);
          if (provider == null)
          {
              Log.i("GetCity","provider is Null!/n");
          }
          else
          {
              Log.i("GetCity", provider.toString() + "/n");
          }
         
          boolean isenable = locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
          if (isenable)
          {
              Log.i("maindesk", "porvider is enable!/n");
          }
          else
          {
              Log.i("maindesk", "provider is unenable");
          }
         
          locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener, Looper.myLooper()); 
          Location mlocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
        if (mlocation == null) { 
             mlocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        }
        if (mlocation == null)
        {
            cityName = null;
            return cityName;
        }
               
        GeoPoint gp = getGeoByLocation(mlocation); 
        Address mAddress = getAddressbyGeoPoint(context, gp);
        cityName = mAddress.getLocality();
        
        return cityName;

}

 

其中Looper.myLooper()就是获取当前线程的消息循环。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值