【问题标题】:Application crashes when I check if GPS sensor present in a sysyem?当我检查系统中是否存在 GPS 传感器时应用程序崩溃?
【发布时间】:2013-04-20 16:30:43
【问题描述】:

我关注了How to check for the presence of a GPS sensor?的答案
并据此进行了更改,但在所有情况下我的应用程序都崩溃了。 我用过

PackageManager pm = getPackageManager();
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);

然后我又修改了我的代码并使用了

LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
if (lm.getProvider(LocationManager.GPS_PROVIDER) == null) {
    Toast.makeText(getApplicationContext(), 
                       "GPS sensor is not available", Toast.LENGTH_LONG).show();
}

但在这两种情况下,我的应用都会崩溃。请告诉我我哪里出错了?

【问题讨论】:

  • 请添加 logtrace 并确保您在 Manifest.xml 中具有正确的权限
  • 我在 Manifest.xml 中添加了所需的权限,并且我正在 Carbonn 移动设备上测试我的应用程序(没有 gps 硬件)。但不幸的是我无法调试,所以我没有 logtrace。
  • @AndroidLover 如果您无法将设备与系统连接,市场上有许多日志收集器应用程序。利用它们并使用 logcat 更新问题。如果没有 logcat stacktrace,我们就会在黑暗中搜索
  • @Sankar V 一旦我得到 logcat,我就会更新我的问题。
  • @Sankar V 非常感谢!我从code.google.com/p/android-log-collector 下载了日志收集器应用程序,并在日志跟踪中发现了我犯的错误。我使用 gps 启动了位置监听器,即使 gps 不可用并且因此我的应用程序崩溃了..

标签: android gps


【解决方案1】:

我在我的代码中发现了错误并进行了必要的更改。
最初我的代码是

if((ProvidePreference.equalsIgnoreCase("BestAvailable")) {
//start both gps and network
     this.LocationMngr.requestLocationUpdates(
                   LocationManager.GPS_PROVIDER,captureFrequencey, 100, this); 
     this.LocationMngr.requestLocationUpdates(
                   LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
}

然后我将代码更改为

if((ProvidePreference.equalsIgnoreCase("BestAvailable") && 
             LocationMngr.getProvider(LocationManager.GPS_PROVIDER) != null)) {
//start both gps and network
     this.LocationMngr.requestLocationUpdates(
                 LocationManager.GPS_PROVIDER,captureFrequencey, 100, this); 
     this.LocationMngr.requestLocationUpdates(
                 LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
} else {
//start network 
     this.LocationMngr.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
}

注意-如果提供程序在系统中不可用,则不应尝试启动它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    相关资源
    最近更新 更多