Search in sources :

Example 1 with ILbsListener

use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.

the class LbsServices method realRequestLocationForAmap.

@SuppressLint("MissingPermission")
private void realRequestLocationForAmap() {
    mlocationClient.setLocationListener(new AMapLocationListener() {

        // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
        // 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后,在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    // 定位成功回调信息,设置相关消息
                    // 获取当前定位结果来源,如网络定位结果,详见定位类型表
                    int locationType = amapLocation.getLocationType();
                    // 获取纬度
                    double latitude = amapLocation.getLatitude();
                    // 获取经度
                    double longitude = amapLocation.getLongitude();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date(amapLocation.getTime());
                    // 定位时间
                    df.format(date);
                    String locationTypeStr;
                    switch(locationType) {
                        // case 0:{locationTypeStr = "定位失败";}
                        case 1:
                            {
                                locationTypeStr = "GPS定位结果";
                            }
                        case 2:
                            {
                                locationTypeStr = "前次定位结果";
                            }
                        // case 3:{locationTypeStr = "缓存定位结果";}
                        case 4:
                            {
                                locationTypeStr = "缓存定位结果";
                            }
                        case 5:
                            {
                                locationTypeStr = "Wifi定位结果";
                            }
                        case 6:
                            {
                                locationTypeStr = "基站定位结果";
                            }
                        // case 7:{locationTypeStr = "离线定位结果";}
                        case 8:
                            {
                                locationTypeStr = "离线定位结果";
                            }
                        case 9:
                            {
                                locationTypeStr = "最后位置缓存";
                            }
                        default:
                            {
                                locationTypeStr = "";
                            }
                    }
                    Log.d(TAG_service, "IntentService onLocationChanged: locationType:" + locationTypeStr + " location" + latitude + " , " + longitude + "");
                    if (iLbsApi.listenerlist == null)
                        return;
                    final int N = iLbsApi.listenerlist.beginBroadcast();
                    for (int i = 0; i < N; i++) {
                        ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
                        if (l != null) {
                            try {
                                List<AppCellInfo> appCellInfos = new ArrayList<>();
                                for (CellInfo cell : telephonyManager.getAllCellInfo()) {
                                    appCellInfos.add(AppCellInfo.convertSysCellInfo(cell));
                                }
                                count += 1;
                                l.onLocationChanged(AppLocation.convertSysLocation(amapLocation), appCellInfos, count);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    iLbsApi.listenerlist.finishBroadcast();
                } else {
                    // 显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                    Log.e(TAG_service, "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo());
                }
            }
        }
    });
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    // 设置定位间隔,单位毫秒,默认为2000ms
    mLocationOption.setInterval(2000);
    mlocationClient.setLocationOption(mLocationOption);
    mlocationClient.startLocation();
}
Also used : AMapLocation(com.amap.api.location.AMapLocation) CellInfo(android.telephony.CellInfo) AppCellInfo(com.jamesfchen.map.model.AppCellInfo) ILbsListener(com.jamesfchen.map.ILbsListener) AMapLocationListener(com.amap.api.location.AMapLocationListener) ArrayList(java.util.ArrayList) List(java.util.List) RemoteException(android.os.RemoteException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint)

Example 2 with ILbsListener

use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.

the class ILbsApiServer method mockRequestLocation.

private void mockRequestLocation() {
    if (listenerlist == null)
        return;
    Type type = new TypeToken<List<L7>>() {
    }.getType();
    List<L7> l7_list = new Gson().fromJson(ResourceUtils.readAssets2String("l7_list.json"), type);
    try {
        Thread.sleep(1500);
        for (L7 l7 : l7_list) {
            double lat = l7.appLocation.lat;
            double lon = l7.appLocation.lon;
            long cid = l7.appCellInfos.get(0).get(0).cid;
            long lac = l7.appCellInfos.get(0).get(0).lac;
            // startservice 回调(onHandleIntent) 和bindservice回调(onServiceConnected) 都是异步,所以sleep模拟onHandleIntent处理耗时,
            // Thread.sleep(500);
            final int N = listenerlist.beginBroadcast();
            for (int i = 0; i < N; i++) {
                ILbsListener l = listenerlist.getBroadcastItem(i);
                if (l == null)
                    continue;
                AppCellInfo appCellInfo = new AppCellInfo();
                appCellInfo.cid = cid;
                appCellInfo.lac = lac;
                appCellInfo.isRegistered = true;
                appCellInfo.isMockData = true;
                AppLocation appLocation = new AppLocation();
                appLocation.lat = lat;
                appLocation.lon = lon;
                appLocation.isMockData = true;
                ++count;
                l.onLocationChanged(appLocation, Collections.singletonList(appCellInfo), count);
            }
            listenerlist.finishBroadcast();
        }
    } catch (InterruptedException | RemoteException e) {
        e.printStackTrace();
    }
}
Also used : Gson(com.google.gson.Gson) L7(com.jamesfchen.map.model.L7) Type(java.lang.reflect.Type) AppCellInfo(com.jamesfchen.map.model.AppCellInfo) ILbsListener(com.jamesfchen.map.ILbsListener) RemoteCallbackList(android.os.RemoteCallbackList) List(java.util.List) AppLocation(com.jamesfchen.map.model.AppLocation) RemoteException(android.os.RemoteException)

Example 3 with ILbsListener

use of com.jamesfchen.map.ILbsListener in project spacecraft-android by JamesfChen.

the class LbsServices method realRequestLocation.

@SuppressLint("MissingPermission")
private void realRequestLocation() {
    List<String> enableProviders = locationManager.getProviders(true);
    List<String> allProviders = locationManager.getAllProviders();
    Log.d("LBS_collection", "onHandleIntent: enable provider:" + enableProviders.toString());
    Log.d("LBS_collection", "onHandleIntent: all provider:" + allProviders);
    String choice;
    boolean gpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (gpsProviderEnabled) {
        choice = LocationManager.GPS_PROVIDER;
    } else {
        boolean netProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (netProviderEnabled) {
            choice = LocationManager.NETWORK_PROVIDER;
        } else {
            choice = LocationManager.PASSIVE_PROVIDER;
        }
    }
    Log.d("LBS_collection", "onHandleIntent: choice provider:" + choice);
    locationManager.requestLocationUpdates(choice, MIN_TIMES, MIN_DISTANCE, new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG_service, "IntentService onLocationChanged: location" + location.getLatitude() + " , " + location.getLongitude());
            if (iLbsApi.listenerlist == null)
                return;
            final int N = iLbsApi.listenerlist.beginBroadcast();
            for (int i = 0; i < N; i++) {
                ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
                if (l != null) {
                    try {
                        List<AppCellInfo> appCellInfos = new ArrayList<>();
                        for (CellInfo cell : telephonyManager.getAllCellInfo()) {
                            appCellInfos.add(AppCellInfo.convertSysCellInfo(cell));
                        }
                        count += 1;
                        l.onLocationChanged(AppLocation.convertSysLocation(location), appCellInfos, count);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
            iLbsApi.listenerlist.finishBroadcast();
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            if (iLbsApi.listenerlist == null)
                return;
            final int N = iLbsApi.listenerlist.beginBroadcast();
            for (int index = 0; index < N; i++) {
                ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(index);
                if (l != null) {
                    try {
                        l.onStatusChanged(s, i, bundle);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
            iLbsApi.listenerlist.finishBroadcast();
        }

        @Override
        public void onProviderEnabled(String s) {
            if (iLbsApi.listenerlist == null)
                return;
            final int N = iLbsApi.listenerlist.beginBroadcast();
            for (int i = 0; i < N; i++) {
                ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
                if (l != null) {
                    try {
                        l.onProviderEnabled(s);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
            iLbsApi.listenerlist.finishBroadcast();
        }

        @Override
        public void onProviderDisabled(String s) {
            if (iLbsApi.listenerlist == null)
                return;
            final int N = iLbsApi.listenerlist.beginBroadcast();
            for (int i = 0; i < N; i++) {
                ILbsListener l = iLbsApi.listenerlist.getBroadcastItem(i);
                if (l != null) {
                    try {
                        l.onProviderDisabled(s);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
            iLbsApi.listenerlist.finishBroadcast();
        }
    }, Looper.getMainLooper());
}
Also used : Bundle(android.os.Bundle) SuppressLint(android.annotation.SuppressLint) CellInfo(android.telephony.CellInfo) AppCellInfo(com.jamesfchen.map.model.AppCellInfo) LocationListener(android.location.LocationListener) AMapLocationListener(com.amap.api.location.AMapLocationListener) ILbsListener(com.jamesfchen.map.ILbsListener) ArrayList(java.util.ArrayList) List(java.util.List) RemoteException(android.os.RemoteException) AMapLocation(com.amap.api.location.AMapLocation) AppLocation(com.jamesfchen.map.model.AppLocation) Location(android.location.Location) SuppressLint(android.annotation.SuppressLint)

Aggregations

RemoteException (android.os.RemoteException)3 ILbsListener (com.jamesfchen.map.ILbsListener)3 AppCellInfo (com.jamesfchen.map.model.AppCellInfo)3 List (java.util.List)3 SuppressLint (android.annotation.SuppressLint)2 CellInfo (android.telephony.CellInfo)2 AMapLocation (com.amap.api.location.AMapLocation)2 AMapLocationListener (com.amap.api.location.AMapLocationListener)2 AppLocation (com.jamesfchen.map.model.AppLocation)2 ArrayList (java.util.ArrayList)2 Location (android.location.Location)1 LocationListener (android.location.LocationListener)1 Bundle (android.os.Bundle)1 RemoteCallbackList (android.os.RemoteCallbackList)1 Gson (com.google.gson.Gson)1 L7 (com.jamesfchen.map.model.L7)1 Type (java.lang.reflect.Type)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1