2 duffy chung Duffy_Chung 于 2016.03.08 23:21 提问

android无法显示地理位置信息,求帮忙看下代码
 package com.example.locationtest;

import java.util.List;

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

public class MainActivity extends Activity 
{
    private TextView positionTextView;
    private LocationManager locationManager;
    private String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        positionTextView=(TextView)findViewById(R.id.position_text_view);
        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        List<String> providerList=locationManager.getProviders(true);
        if(providerList.contains(LocationManager.GPS_PROVIDER))
        {
            provider=LocationManager.GPS_PROVIDER;
        }
        else if(providerList.contains(LocationManager.NETWORK_PROVIDER))
        {
            provider=LocationManager.NETWORK_PROVIDER;
        }
        else
        {
            Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
            return;
        }
        Location location=locationManager.getLastKnownLocation(provider);
        if(location != null)
        {
            showLocation(location);
        }
        locationManager.requestLocationUpdates(provider,5000, 1, locationListener);
    }

    protected void onDestroy()
    {
        super.onDestroy();
        if(locationManager != null)
        {
            locationManager.removeUpdates(locationListener);
        }
    }

    LocationListener locationListener=new LocationListener()
        {
            @Override
            public void onStatusChanged(String provider,int status,Bundle extras)
            {

            }

            @Override
            public void onProviderEnabled(String provider) 
            {

            }
            @Override
            public void onProviderDisabled(String provider) 
            {

            }

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


    private void showLocation(Location location) 
    {
        String currentPosition="latitude is"+location.getLatitude()+"\n"+"longitude is"+location.getLongitude();
        positionTextView.setText(currentPosition);
    }
}

5个回答

u011249920
u011249920   2016.03.09 09:11

你配置文件中有没有加权限?

VNanyesheshou
VNanyesheshou   2016.03.09 09:33
dcxy0
dcxy0   Ds   Rxr 2016.03.09 09:48
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.09 10:30

1.可能没加权限
2.可能没联网也没开GPS
3.可能你把权限关了

crazy1235
crazy1235   Ds   Rxr 2016.03.09 10:36
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!