xiaoxiaoaishang 2012-11-27 03:47 采纳率: 100%
浏览 2894
已采纳

Android GPS是返回过时的信息

在我现在的应用程序中,返回的位置信息是过时的。我使用一个方法来获得更新基于最小时间/更新之间的距离。然而,比方说,我把手机关掉,开车去另一个城市,然后把手机打开,在这种情况下我的GPS就会关闭。我怎么做才能让app获得当前的位置,而不是getLastKnownLocation()

我听说过locationListener,但是我获得信息关于怎么用它都很模糊
下边是我的代码,希望能对找到问题有点帮助:

public class GPSHandling extends Service implements LocationListener{

    private final Context myContext;

    //gps状态标记
    public boolean isGPSEnabled = false;

    //网络状态标记
    public boolean isNetworkEnabled = false;

    //用于决定是否我能通过网络或者是GPS获得位置
    public boolean canGetLocation = false;

    Location myloc;

    public double latitude;
    public double longitude;

    public int MIN_TIME_BTWN_UPDATE = 500*10;  
    public int MIN_DISTANCE_BTWN_UPDATE = 10;  
    protected LocationManager locManager;

    public GPSHandling(Context context){
        this.myContext= context;
        getLocation();
    }
    public Location getLocation(){
        try{
            locManager =(LocationManager) myContext.getSystemService(LOCATION_SERVICE);

            // 现在获得gps状态
            isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //获得网络状态
            isNetworkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled){
                (this.cangetlocation = false but its already set to that by default)
            }
            else{
                this.canGetLocation=true;
                //首先从网络提供商获得值给locManager,发给parameters告诉他们何时更新
                if(isNetworkEnabled){
                    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "network");
                    //如果我们成功了,然后检查位置信息是不是空,试着从manager处获得当前位置信息
                    if (locManager != null){
                        myloc =locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            // 在得到当前的位置后,试着获得经纬度
                            if (myloc != null){
                                latitude = myloc.getLatitude();
                                longitude = myloc.getLongitude();
                                              }
                                           }
                                    }
                //现在从GPS提供商获得值给locManager,发给parameters告诉什么时候更新】
                if(isGPSEnabled){
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "GPS");
                }
                //如果我们成功了,然后检查位置信息是不是空,试着从manager处获得当前位置信息
                    if(locManager!= null){
                        myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                    //在得到当前的位置后,试着获得经纬度
                        if(myloc != null){
                            latitude = myloc.getLatitude();
                            longitude = myloc.getLongitude();
                        }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myloc;
    }
    //通过类方法获得当前纬度的数据
    public double getMyLatitude(){
        if (myloc!= null){
            latitude = myloc.getLatitude();
        }
        return latitude;
    }
    //通过类方法获得当前经度的数据
    public double getMyLongitude(){
        if (myloc != null ){
            longitude = myloc.getLongitude();
        }
        return longitude;
    }

    //用这个方法看app是否能获得当前的坐标
    public boolean canGetMyLocation(){
        return this.canGetLocation;
    }

    public void showGPSDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(myContext);
        //设置Dialog Title 
        alert.setTitle("Location Setting");
        // 设置Dialog Message
        alert.setMessage("GPS is not enabled, do you want to enable this now in the settins menue?");

        //给dialog设置icon
        //alert.setIcon(R.drawable.)

        //当点击的时候设置按钮
        alert.setPositiveButton("Settins", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                myContext.startActivity(intent);
            }
        });
        //当点击的时候取消按钮
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        //显示警告dialog
        alert.show();
    }

    public void stopUsingGPS(){
        if(locManager !=null){
            locManager.removeUpdates(GPSHandling.this);
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        // TODO自动生成方法存根

    }

    @Override
    public void onProviderDisabled(String provider) {
        //  TODO自动生成方法存根

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO自动生成方法存根

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO自动生成方法存根

    }

    @Override
    public IBinder onBind(Intent intent) {
        //  TODO自动生成方法存根
        return null;
    }
}
  • 写回答

1条回答 默认 最新

  • liangchichexin 2012-11-27 05:33
    关注

    您已经在您的代码中使用了一部分的LocationListener了。你只是没有完全实现代码。
    在你的代码中下边的这个是空的:

    @Override
    public void onLocationChanged(Location location) {
        // TODO自动生成方法存根
    
    }
    

    这个方法在每次GPS生成一个新的位置信息的时候都要调用的。
    你应该把它完成的:

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
    

    就是以上这些了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?