Android GPS检查崩溃?

时间:2012-10-12 18:31:35

标签: android crash gps

从昨天开始广泛搜索,找不到任何有这个问题的人:

我创建了一个GPS检查功能,它只返回true或false,指示GPS是否打开。这在Android 2.2和4.1的模拟器和运行4.1的手机上运行得很好。

该功能如下:

    private Boolean checkGPSEnabled(){
        try{
            String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if(provider.toLowerCase().contains("gps")){
                return true;
            }
        }
        catch(IllegalArgumentException e){
            return true;
        }

        return false;
    }

问题是,当在Android模拟器上执行此检查时启用GPS时,模拟器会彻底崩溃。

以下是发生错误时在LogCat中打印的内容:

10-12 15:23:45.593: W/dalvikvm(899): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-12 15:23:45.644: E/AndroidRuntime(899): FATAL EXCEPTION: main
10-12 15:23:45.644: E/AndroidRuntime(899): java.lang.IllegalStateException: Could not execute method of the activity
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.view.View$1.onClick(View.java:3591)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.view.View.performClick(View.java:4084)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.view.View$PerformClick.run(View.java:16966)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.os.Handler.handleCallback(Handler.java:615)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.os.Looper.loop(Looper.java:137)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-12 15:23:45.644: E/AndroidRuntime(899):  at java.lang.reflect.Method.invokeNative(Native Method)
10-12 15:23:45.644: E/AndroidRuntime(899):  at java.lang.reflect.Method.invoke(Method.java:511)
10-12 15:23:45.644: E/AndroidRuntime(899):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-12 15:23:45.644: E/AndroidRuntime(899):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 15:23:45.644: E/AndroidRuntime(899):  at dalvik.system.NativeStart.main(Native Method)
10-12 15:23:45.644: E/AndroidRuntime(899): Caused by: java.lang.reflect.InvocationTargetException
10-12 15:23:45.644: E/AndroidRuntime(899):  at java.lang.reflect.Method.invokeNative(Native Method)
10-12 15:23:45.644: E/AndroidRuntime(899):  at java.lang.reflect.Method.invoke(Method.java:511)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.view.View$1.onClick(View.java:3586)
10-12 15:23:45.644: E/AndroidRuntime(899):  ... 11 more
10-12 15:23:45.644: E/AndroidRuntime(899): Caused by: java.lang.IllegalArgumentException: requested provider network doesn't exisit
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.os.Parcel.readException(Parcel.java:1429)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.os.Parcel.readException(Parcel.java:1379)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.location.LocationManager._requestLocationUpdates(LocationManager.java:660)
10-12 15:23:45.644: E/AndroidRuntime(899):  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:482)
10-12 15:23:45.644: E/AndroidRuntime(899):  at com.desmill.taxi.activity.TaxiCaller.callTaxi(TaxiCaller.java:52)
10-12 15:23:45.644: E/AndroidRuntime(899):  ... 14 more

有什么想法吗?如何让它显示其他错误... ...更多......? 谢谢!

2 个答案:

答案 0 :(得分:1)

你可以将logcat输出转储到一个文本文件,你应该能够在那里读取它,虽然我不是正面的。只需打开一个终端窗口,导航到包含adb可执行文件的目录(除非它在您的PATH中)并输入:

adb logcat -d > logcat.txt

清除logcat内容的另一个有用命令是:

adb logcat -c

答案 1 :(得分:0)

这适用于2.2 +

    try {
        LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {

    }