Android - 无法在模拟器上获取 gps 位置

2023-06-26Java开发问题
11

本文介绍了Android - 无法在模拟器上获取 gps 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在 android 模拟器上使用 gps,我有以下代码:

i'm tring to use the gps on the android emulator, i've the following code:

public class NL extends Activity {

 private LocationManager locmgr = null;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nl);

        locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = locmgr.getBestProvider(crit, true);
        Location loc = locmgr.getLastKnownLocation(provider);


        Toast msg = Toast.makeText(this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
        msg.show();
    }
}

我在清单中添加了以下行:

i've added the following line at the manifest:

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

我已经使用 DDMS 方法和 geo fix 方法设置了 gps 位置,但是当我运行代码时,我在 Toast 行得到一个 NullPointerExeption,可能是因为 loc 为空.

and i've set the gps location with the DDMS method and either with the geo fix method, but when i run the code, i get a NullPointerExeption at the Toast line, probably cause loc is null.

我不明白错误出在哪里...你能帮帮我吗?

I don't understand where the error is... can you help me please?

更新!

感谢您的帮助,现在我使用以下代码并且没有收到任何错误,但它没有运行 onChangeLocation 内的代码...它没有运行 Toast 并且不返回任何消息在日志中!

Thanks for your help, now i use the following code and i don't get any error, but it doesn't run the code inside onChangeLocation... it doesn't run the Toast and don't return any message in the log!

 public class NL extends Activity {

        private LocationManager locmgr = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.nl);

            locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

         // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location loc) {
                  // Called when a new location is found by the network location provider.
                    Log.i("NOTIFICATION","onLocationChenged Triggered");

                    Toast msg = Toast.makeText(NetworkLocator.this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
                    msg.show();
                }

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

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

            // Register the listener with the Location Manager to receive location updates
            locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }
    }

谢谢!

推荐答案

找到解决方案:

LocationManager.NETWORK_PROVIDER 错误.

LocationManager.NETWORK_PROVIDER is WRONG.

更正:LocationManager.GPS_PROVIDER

correction: LocationManager.GPS_PROVIDER

这篇关于Android - 无法在模拟器上获取 gps 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13