Android 当我检查系统中是否存在GPS传感器时,应用程序崩溃?
我按照Android 当我检查系统中是否存在GPS传感器时,应用程序崩溃?,android,gps,Android,Gps,我按照 并且做了相应的更改,但在所有情况下,我的应用程序都会崩溃。 我曾经 然后我再次修改了代码并使用 LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE); if (lm.getProvider(LocationManager.GPS_PROVIDER) == null) { Toast.makeText(getApplicationContext(),
并且做了相应的更改,但在所有情况下,我的应用程序都会崩溃。 我曾经 然后我再次修改了代码并使用
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();
}
但在这两种情况下,我的应用程序都崩溃了。请告诉我哪里出错了?我在代码中发现了错误,并进行了必要的更改。
最初我的代码是
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);
}
注意-如果提供程序在系统中不可用,则不应尝试启动它。请添加日志跟踪,并确保您在Manifest.xml中拥有正确的权限。我已在Manifest.xml中添加了所需的权限,我正在Carbon mobile(无gps硬件)上测试我的应用程序。但不幸的是,我无法调试,所以我没有logtrace。@AndroidLover如果您无法将设备与系统连接,市场上有许多日志收集器应用程序可用。利用它们并用logcat更新问题。如果没有logcat stacktrace,我们将无法在dark@SankarV我一得到logcat,我就更新我的问题。@Sankar V非常感谢,伙计!!我已经从下载了日志收集器应用程序,并在日志跟踪中发现了我犯的错误。我开始使用gps定位监听器,即使gps不可用,我的应用程序也因此崩溃。。
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);
}