Android 经度和纬度不显示

Android 经度和纬度不显示,android,gps,Android,Gps,我正在尝试使用GPS显示用户的位置。我编写了以下代码,以便如果设备上禁用了GPS,则用户应通过转到设置来启用它。如果启用,则应显示用户的当前位置。我正在android设备上运行应用程序。我在运行应用程序时没有收到任何错误,但不会显示位置。请帮忙 ------------主要活动-------------------- package com.android.disasterAlertApp; import android.app.Activity; import android.app.Ale

我正在尝试使用GPS显示用户的位置。我编写了以下代码,以便如果设备上禁用了GPS,则用户应通过转到设置来启用它。如果启用,则应显示用户的当前位置。我正在android设备上运行应用程序。我在运行应用程序时没有收到任何错误,但不会显示位置。请帮忙 ------------主要活动--------------------

package com.android.disasterAlertApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
  }
  else{
    showGPSDisabledAlertToUser();
   }
  }

  private void showGPSDisabledAlertToUser(){
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  alertDialogBuilder.setMessage("GPS is disabled.Do you want to enable it?")
 .setCancelable(false)
 .setPositiveButton("Goto Settings Page To Enable GPS",
 new DialogInterface.OnClickListener(){
 public void onClick(DialogInterface dialog, int id){
 Intent callGPSSettingIntent = new Intent(
 android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
 startActivity(callGPSSettingIntent);
    }
  });
 alertDialogBuilder.setNegativeButton("Cancel",
 new DialogInterface.OnClickListener(){
 public void onClick(DialogInterface dialog, int id){
 dialog.cancel();
  }
 });
 AlertDialog alert = alertDialogBuilder.create();
 alert.show();
 }

  private class mylocationlistener implements LocationListener {
    public void onLocationChanged(Location location) {
      if (location != null) {
   Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(MainActivity.this,
    location.getLatitude() + "" + location.getLongitude(),
    Toast.LENGTH_LONG).show();
     }
   }

   public void onProviderDisabled(String provider) {
  }
   public void onProviderEnabled(String provider) {
  }
   public void onStatusChanged(String provider, int status, Bundle extras) {
  }
  }
 } 

 ------------Manifest-----------------

 <uses-sdk android:minSdkVersion="8" />

<application
   android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
    android:name=".Splash"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

       <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.android.disasterAlertApp.MAINACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
     </activity>

 </application>   
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission>
 </manifest>
package com.android.disasterAlertApp;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
/**在首次创建活动时调用*/
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
LocationListener ll=新的mylocationlistener();
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,0,0,ll);
if(locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){
Toast.makeText(这是“在您的设备中启用GPS”,Toast.LENGTH_SHORT.show();
}
否则{
showGPSDisabledAlertToUser();
}
}
私有void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(此);
alertDialogBuilder.setMessage(“GPS已禁用。是否要启用它?”)
.setCancelable(错误)
.setPositiveButton(“转到设置页面以启用GPS”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
Intent callgpssettingcontent=新Intent(
android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings);
开始触觉(CallGPSSettingContent);
}
});
alertDialogBuilder.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
AlertDialog alert=alertDialogBuilder.create();
alert.show();
}
私有类mylocationlistener实现LocationListener{
已更改位置上的公共无效(位置){
如果(位置!=null){
Log.d(“位置已更改”,LOCATION.getLatitude()+);
Log.d(“位置已更改”,LOCATION.getLongitude()+”);
Toast.makeText(MainActivity.this,
location.getLatitude()+“”+location.getLatitude(),
Toast.LENGTH_LONG).show();
}
}
公共无效onProviderDisabled(字符串提供程序){
}
公共无效onProviderEnabled(字符串提供程序){
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
}
} 
------------显示-----------------

获取GPS位置需要时间,如果您在建筑物内很难获取GPS值,如果设备位于建筑物外,我们可以获取GPS值。

在清单文件中添加此权限

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


确保您的设备GPS硬件正常工作。我有两部全新的LG Lucid手机,但GPS在这两部手机上都不工作。在“位置设置”中,仅选中GPS框,然后启动谷歌地图,查看它是否可以获取位置。

添加了互联网权限,但仍然不起作用