0

我正在尝试此代码用于 android 位置,但是当我运行模拟器时,它说“不幸的是位置已停止”......有什么想法吗?这是 MainAcivity 的代码(我按照本教程http://www.vogella.com/articles/AndroidLocationAPI/article.html#locationapi_gpsenable

    public class MainActivity extends Activity implements LocationListener {
      private TextView latituteField;
      private TextView longitudeField;
      private LocationManager locationManager;
      private String provider;

/** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

// Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }
      }

/* Request updates at startup */
      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

/* Remove the locationlistener updates when Activity is paused */
      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      @Override
      public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
      }

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

      }

      @Override
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }
    }   
4

3 回答 3

2

您必须手动输入模拟器的位置。有几种选择

选项 1:使用 DDMS

  • 转到您的 android/tools 目录,然后启动 DDMS 工具

  • 选择模拟器控制选项卡

  • 用经度和纬度值填充位置控制字段

  • 按发送按钮

选项 2:使用远程登录

  • 打开命令外壳
  • 使用以下命令连接到模拟器:telnet localhost
  • 进入固定位置执行命令:

geo fix <经度> <纬度> [<高度>]

选项 3:使用 3rd 方应用程序

您可以尝试使用 android-gps-emulator应用程序来设置位置。可能还有其他类似的应用

资源

于 2013-03-16T03:54:03.730 回答
1

试试这个代码我可能对你有帮助..

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

还设置地理位置 您可以使用 Eclipse 的“DDMS”透视图将您的地理位置发送到模拟器或连接的设备。要打开此 Perspective,请选择 Window → Open Perspective → Other... → DDMS。

在模拟器控制部分,您可以输入地理坐标并按发送按钮。

在此处输入图像描述

于 2013-03-16T03:45:22.353 回答
1

您可以使用 Genymotion 模拟器,在虚拟设备上设置位置非常容易。

于 2016-02-02T00:07:19.910 回答