【问题标题】:I Couldn't Reach Change Location Value in Android我无法在 Android 中更改位置值
【发布时间】:2013-05-14 22:07:13
【问题描述】:

我很抱歉我的简单问题,但我是这个话题的新手。我有适用于 Android 的位置类,我使用 Toast 消息到达我的位置,但我无法达到我的位置值其他类。例如;

我的位置类

import android.app.AlertDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MyLocation{
    private LocationManager locationManager;
    LocationListener locationListener;
    private Context ourContext;

    public MyLocation(Context context){

        this.ourContext = context;

        locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();

        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(!gps){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 100, locationListener);
            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("GPS Disabled");
            alertDialog.setMessage("You can enable GPS for settings");
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();     
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 100, locationListener);
        }

    }

    public class MyLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Bundle bundle = new Bundle();

            lat = location.getLatitude();
            lon = location.getLongitude();
            Toast.makeText(ourContext, lat +" ve "+ lon, Toast.LENGTH_LONG).show();

            bundle.putString("lat", lat.toString());
            bundle.putString("lon", lon.toString());
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        public String getLat(){
            return lat.toString();
        }

        public String getLon(){
            return lon.toString();
        }

        private Double lat;
        private Double lon;
    }

我叫

MyLocation myLocation = new MyLocation(getActivity());
Runnable runnable = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(bundle.getString("lon") != null){
                    tv1.setText("Lon : " + bundle.getString("lon"));
                    tv2.setText("Lat : " + bundle.getString("lat"));
                }

            }
        };

        new Thread(runnable).start();

但每次我的捆绑包返回空值。我能做些什么 ?

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    因为您构建了一个新的 Bundle,因此它可以检索您保存在 Activity 中的数据。

    您可以执行以下操作:

    public class MyLocationListener implements LocationListener{
    
       Bundle bundle = new Bundle();
    
       public Bundle getBundle(){
            return bundle;
       }
    
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
    
                lat = location.getLatitude();
                lon = location.getLongitude();
                Toast.makeText(ourContext, lat +" ve "+ lon, Toast.LENGTH_LONG).show();
    
                bundle.putString("lat", lat.toString());
                bundle.putString("lon", lon.toString());
            }
          //other method
    
    }
    
    public class MyLocation{
        private LocationManager locationManager;
        LocationListener locationListener;
        private Context ourContext;
    
        public MyLocation(Context context){
    
            this.ourContext = context;
    
            locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
            locationListener = new MyLocationListener();
    
            boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            if(!gps){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 100, locationListener);
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("GPS Disabled");
                alertDialog.setMessage("You can enable GPS for settings");
                alertDialog.setIcon(R.drawable.ic_launcher);
                alertDialog.show();     
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 100, locationListener);
            }
    
    public Bundle getBundle(){
      if(locationListener != null)
          return locationListener.getBundle();
       else 
           return null;
    }
        }
    

    那就做吧:

        Bundle bundle = myLocation.getBundle();
        if(bundle != null){
            tv1.setText("Lon : " + bundle.getString("lon"));
            tv2.setText("Lat : " + bundle.getString("lat"));
    }
    

    编辑:

    更改private LocationListener locationListener;

    private MyLocationListener locationListener;

    【讨论】:

    • 我尝试像这样更改我的代码,但我无法从 MyLocation 类访问 Bundle。我有 MyLocation 类和内部类实现 LocationListener 之后我编写了 getBundle 函数和“返回 locationListener.getBundle();”但这不是从内部类到达 Bundle 中的 Bundle。为什么我不知道..谢谢回答
    【解决方案2】:

    我所有的代码;

    http://p1305.hizliresim.com/19/h/n2zwm.png

    感谢您的帮助

    package com.example.postproject;
    
    import android.app.AlertDialog;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class MyLocation{
        private LocationManager locationManager;
        private LocationListener locationListener;
        private Context ourContext;
    
        public MyLocation(Context context){
    
            this.ourContext = context;
    
            locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
            locationListener = new MyLocationListener();
    
            boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            if(!gps){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 100, locationListener);
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("GPS Disabled");
                alertDialog.setMessage("You can enable GPS for settings");
                alertDialog.setIcon(R.drawable.ic_launcher);
                alertDialog.show();     
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 100, locationListener);
            }
        }
    
        public Bundle getBundle(){
            if(locationListener != null)
                return locationListener.getBundle();
            else
                return null;
        }
    
        public class MyLocationListener implements LocationListener{
            Bundle bundle = new Bundle();
    
            public Bundle getBundle(){
                return bundle;
            }
    
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                lat = location.getLatitude();
                lon = location.getLongitude();
                Toast.makeText(ourContext, lat +" ve "+ lon, Toast.LENGTH_LONG).show();
                bundle.putString("lat", lat.toString());
                bundle.putString("lon", lon.toString());
            }
    
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
    
            }
    
            public String getLat(){
                return lat.toString();
            }
    
            public String getLon(){
                return lon.toString();
            }
    
            private Double lat;
            private Double lon;
    
        }
    
    
    }
    

    我用

    MyLocation myLocation = new MyLocation(getActivity());
    
            Bundle bundle = myLocation.getBund();
            tv1.setText("Lon : " + bundle.getString("lon"));
            tv2.setText("Lat : " + bundle.getString("lat"));
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2016-11-06
      • 2017-09-06
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多