Android的GPS位置 - 它不会去onLocationChanged [英] Android GPS Location- It doesn't go to onLocationChanged

查看:113
本文介绍了Android的GPS位置 - 它不会去onLocationChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到的GPS位置value.I已经写了code.But我没有显示消息。我在光的装置没有emulator.Device包含GPS和放大器测试;它的启用。 &安培;没有互联网连接也。

I want to get the GPS location value.I have wrote code.But i didn't show message. I have tested in deveice not emulator.Device contain GPS & its enable. & there is no internet connection also.

请告诉我什么是错误的,我code

Please tell me what is wrong in my code

public class AndroidGPSSampleActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
    protected LocationManager locationManager;
    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
        );
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCurrentLocation();
            }
        });
    }

    protected void showCurrentLocation() {

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );

            Toast.makeText(AndroidGPSSampleActivity.this, "Sample test",
                Toast.LENGTH_LONG).show();
            Toast.makeText(AndroidGPSSampleActivity.this, message,
                Toast.LENGTH_LONG).show();
        }
    }

    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            System.out.println("==onProviderEnabled=" + s);
            Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
        }
    }

和权限

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

总是位置给空值。它没有去 onLocationChanged 方法...

always Location give null value. It didn't go to onLocationChanged method...

请帮我从这个问题...

Please help me out from this issue...

推荐答案

我适应的我在哪里教程..希望这将有助于

I adapt where am i tutorial.. hope it would help

P.S。我写它没有测试..从技术上说,它的工作..我想

p.s. I write it without testing .. technically,it would work.. i guess

private Location loca;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locman;
        String context = Context.LOCATION_SERVICE;
        locman = (LocationManager)getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String  provider = locman.getBestProvider(criteria, true);

        loca = locman.getLastKnownLocation(provider);

        updateWithNewLocation(loca);

        locman.requestLocationUpdates(provider, 1000, 1, locationListener);


        retrieveLocationButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
           String message = "My location : " + updateWithNewLocation(loca);

        Toast.makeText(AndroidGPSSampleActivity.this, message,
                Toast.LENGTH_LONG).show();
        }
});        


    }

    private final LocationListener locationListener = new LocationListener() {

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

        }

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

        }

        @Override
        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        @Override
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }
    };


    private string updateWithNewLocation(Location location){

        if(location!=null){

            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;

        }else{
            latLongString = "No location found";

        }
           return latLongString;

    }

这篇关于Android的GPS位置 - 它不会去onLocationChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆