为啥此代码无法通过 GPS 获取位置?

Posted

技术标签:

【中文标题】为啥此代码无法通过 GPS 获取位置?【英文标题】:Why is this code to get the location via GPS not working?为什么此代码无法通过 GPS 获取位置? 【发布时间】:2011-04-15 07:15:55 【问题描述】:

即使启用了 GPS 并且所有位置管理器和侦听器都已正确设置,我也无法获取位置。到目前为止,这是我所拥有的:

public class GpsTest extends Activity  
     private TextView text; 
     private LocationManager manager; 
     private LocationListener listener;
     private  Location myLocation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView)findViewById(R.id.textView1);
 try
 
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    myLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //"gps"
    text.setText(("Lat: " + myLocation.getLatitude() 
                + "\nLLong: " + myLocation.getLongitude())); 
    listener = new MyLocationListener(); 
    manager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, 
                                    0, 
                                    1, 
                                    listener);
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER))  
        Toast.makeText(this, "GPS is enable. ", Toast.LENGTH_SHORT).show();  
    
    else createGpsDisabledAlert();   
 catch(Exception ex)
 
     createGpsDisabledAlert(); 
 
         


public void onclicklistener(View v)
    try
    
      text.setText(("Lat: " + myLocation.getLatitude() 
              + "\nLLong: " + myLocation.getLongitude()));
    catch(Exception ex)
    
     Toast.makeText(this, "Error while getting locations. "+ex.toString(), Toast.LENGTH_SHORT).show();   
    
  
  private void createGpsDisabledAlert()  
      AlertDialog.Builder builder = new AlertDialog.Builder(this);  
      builder.setMessage("GPS is disabled! Would you like to enable it?")  
           .setCancelable(false)  
           .setPositiveButton("Enable GPS",  
                new DialogInterface.OnClickListener()  
                public void onClick(DialogInterface dialog, int id)  
                     showGpsOptions();  
                  
           );  
           builder.setNegativeButton("No Thanks",  
                new DialogInterface.OnClickListener()  
                public void onClick(DialogInterface dialog, int id)  
                     dialog.cancel();  
                  
           );  
      AlertDialog alert = builder.create();  
      alert.show();  
         
  private void showGpsOptions()  
      Intent gpsOptionsIntent = new Intent(  
              android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
      startActivity(gpsOptionsIntent);  
    



   private class MyLocationListener implements LocationListener 
                public void onLocationChanged(Location location)  

                        // TODO Auto-generated method stub 
                        if (location != null) 
                                text.setText(("Lat: " + location.getLatitude() 
                                        + "\nLLong: " + location.getLongitude())); 
                         
                        Toast.makeText(GpsTest.this, ""+location, Toast.LENGTH_SHORT).show();
                 
                public void onProviderDisabled(String provider)  
                        // TODO Auto-generated method stub 
                 
                public void onProviderEnabled(String provider)  
                        // TODO Auto-generated method stub

                
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
                    // TODO Auto-generated method stub

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

【问题讨论】:

【参考方案1】:

你必须像这样使用带有他的 run() 方法的线程(是西班牙语,我希望这对你来说不是问题)

@Override
public void run() 

    mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

    if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
        Looper.prepare();
        mToast.Make(getContext(),"GPS",0);
        mLocationListener = new MyLocationListener();
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
        Looper.loop(); 
        Looper.myLooper().quit(); 

     else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        Looper.prepare();
        mToast.Make(getContext(),"Triangulacion",0);
        mLocationListener = new MyLocationListener();
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
        Looper.loop();
        Looper.myLooper().quit();
    else
        mToast.Make(context,"No se encuentra señal se procede a mandar un mensaje normal",0);
        Looper.prepare();
        handlerNormal.sendEmptyMessage(0);
        Looper.loop();
        Looper.myLooper().quit();
       

   

【讨论】:

感谢 GPS 可以独立工作还是需要网络来获取位置,我的设备没有数据网络。【参考方案2】:

您是在模拟器还是真机上进行测试?如果您在模拟器上进行测试,请查看http://www.javacodegeeks.com/2010/09/android-location-based-services.html 并阅读有关位置控制的 DDMS 部分。您需要为第一次出现提供一些虚拟位置数据,然后它将获取您当前的位置。

【讨论】:

我正在使用 Spice MI-300 android 1.6

以上是关于为啥此代码无法通过 GPS 获取位置?的主要内容,如果未能解决你的问题,请参考以下文章

如果 GPS 被禁用,位置将无法进入三星手机

无法使用通用代码获取 GPS 位置

我无法获取 GPS 位置

无法访问 GPS。应用程序使用网络获取位置

Android GPS 无法获取我的位置

FusedLocationProviderClient 无法通过 3G 或 4G 网络获取 GPS 位置,除非先连接到 WIFI 网络