0

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

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>
4

3 回答 3

0

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

于 2012-05-17T06:02:10.073 回答
0

确保您的设备 GPS 硬件正常工作。我有 2 部全新的 LG Lucid 手机,但 GPS 无法在其中任何一部上工作。在位置设置上只检查 GPS 框,然后启动谷歌地图,看看它是否可以获取位置。

于 2012-05-18T07:05:06.683 回答
0

在清单文件中添加此权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
于 2012-05-17T06:19:47.060 回答