简体   繁体   English

位置来源设置意图仅适用于<4.0的所有设备

[英]Location source settings intent works only with all devices < 4.0

Hi below is my code for checking gps settings which works perfectly on all devices with android 2.3.6 and lower , but not in ICS and Jellybean. 您好,下面是我的代码,用于检查gps设置,该代码可在所有Android 2.3.6及更低版本的设备上完美运行,但不适用于ICS和Jellybean。 When i check for GPS settings enabled or not on the device and start the location settings intent and enable the GPS and return to my app , when i check again for GPS it still shows disabled and force closes the app 当我检查设备上是否启用了GPS设置并启动位置设置意图并启用GPS并返回到我的应用程序时,当我再次检查GPS时,它仍然显示已禁用并强制关闭该应用程序

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

        public Location getLocation() {
                try {





                    locationManager = (LocationManager) mContext
                            .getSystemService(LOCATION_SERVICE);

                    // getting GPS status
                    isGPSEnabled = locationManager
                            .isProviderEnabled(LocationManager.GPS_PROVIDER);

                    // getting network status
                    isNetworkEnabled = locationManager
                            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                   // if (!isGPSEnabled && !isNetworkEnabled) 
                    if (!isGPSEnabled && !isNetworkEnabled)
                    {
                        // no network provider is enabled
                    } else {
                        this.canGetLocation = true;
                        // First get location from Network Provider
                        if (isNetworkEnabled) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.NETWORK_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("Network", "Network");
                            if (locationManager != null) {
                             location = locationManager


                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                        // if GPS Enabled get lat/long using GPS Services
                        if (isGPSEnabled) {
                            if (location == null) {
                                locationManager.requestLocationUpdates(
                                        LocationManager.GPS_PROVIDER,
                                        MIN_TIME_BW_UPDATES,
                                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                                Log.d("GPS Enabled", "GPS Enabled");
                                if (locationManager != null) {
                                    location = locationManager
                                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                    if (location != null) {
                                        latitude = location.getLatitude();
                                        longitude = location.getLongitude();

                                    }
                                }
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }



                return location;
            }





        /**
         * Function to get latitude
         * */
        public double getLatitude(){
            if(location != null){
                latitude = location.getLatitude();
            }

            // return latitude
            return latitude;
        }

        /**
         * Function to get longitude
         * */
        public double getLongitude(){
            if(location != null){
                longitude = location.getLongitude();
            }

            // return longitude
            return longitude;
        }

        public boolean canGetLocation() {
            return this.canGetLocation;
        }

        /**
         * Function to show settings alert dialog
         * */
        public void showSettingsAlert(){
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

            // Setting Dialog Title
            alertDialog.setTitle("GPS is settings");

            // Setting Dialog Message
            alertDialog.setMessage("GPS is not enabled. Do you want to go to
                settings menu?");

            // Setting Icon to Dialog
            //alertDialog.setIcon(R.drawable.delete);

            // On pressing Settings button
            alertDialog.setPositiveButton("Settings", new 

                       DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    Intent intent = new 
                            Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            });

            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel", new 
             DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();
        }

And this is what i get in logcat 这就是我在logcat中得到的

                01-09 14:53:14.190: E/StrictMode(555): class com.android.settings.Settings$LocationSettingsActivity; instances=2; limit=1
01-09 14:53:14.190: E/StrictMode(555): android.os.StrictMode$InstanceCountViolation: class com.android.settings.Settings$LocationSettingsActivity; instances=2; limit=1
01-09 14:53:14.190: E/StrictMode(555):  at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
01-09 14:53:14.637: E/StrictMode(555): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
01-09 14:53:14.637: E/StrictMode(555): java.lang.Throwable: Explicit termination method 'close' not called
01-09 14:53:14.637: E/StrictMode(555):  at dalvik.system.CloseGuard.open(CloseGuard.java:184)
01-09 14:53:14.637: E/StrictMode(555):  at android.content.ContentResolver$CursorWrapperInner.<init>(ContentResolver.java:1581)
01-09 14:53:14.637: E/StrictMode(555):  at android.content.ContentResolver.query(ContentResolver.java:320)
01-09 14:53:14.637: E/StrictMode(555):  at com.android.settings.LocationSettings.onStart(LocationSettings.java:62)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.Fragment.performStart(Fragment.java:1493)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:981)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1700)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.Activity.performStart(Activity.java:4481)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-09 14:53:14.637: E/StrictMode(555):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 14:53:14.637: E/StrictMode(555):  at android.os.Looper.loop(Looper.java:137)
01-09 14:53:14.637: E/StrictMode(555):  at android.app.ActivityThread.main(ActivityThread.java:4340)
01-09 14:53:14.637: E/StrictMode(555):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 14:53:14.637: E/StrictMode(555):  at java.lang.reflect.Method.invoke(Method.java:511)
01-09 14:53:14.637: E/StrictMode(555):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 14:53:14.637: E/StrictMode(555):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 14:53:14.637: E/StrictMode(555):  at dalvik.system.NativeStart.main(Native Method)
01-09 14:53:14.647: W/CursorWrapperInner(555): Cursor finalized without prior close()
01-09 14:53:14.677: E/System(555): Uncaught exception thrown by finalizer
01-09 14:53:14.877: E/System(555): java.lang.IllegalStateException: Binder has been finalized!
01-09 14:53:14.877: E/System(555):  at android.os.BinderProxy.transact(Native Method)
01-09 14:53:14.877: E/System(555):  at android.database.BulkCursorProxy.close(BulkCursorNative.java:288)
01-09 14:53:14.877: E/System(555):  at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:133)
01-09 14:53:14.877: E/System(555):  at android.database.CursorWrapper.close(CursorWrapper.java:45)
01-09 14:53:14.877: E/System(555):  at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1586)
01-09 14:53:14.877: E/System(555):  at android.content.ContentQueryMap.close(ContentQueryMap.java:173)
01-09 14:53:14.877: E/System(555):  at android.content.ContentQueryMap.finalize(ContentQueryMap.java:179)
01-09 14:53:14.877: E/System(555):  at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
01-09 14:53:14.877: E/System(555):  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-09 14:53:14.877: E/System(555):  at java.lang.Thread.run(Thread.java:856)

First of all make sure that you are not importing in the wrong settings library. 首先,请确保您没有导入错误的设置库。 You should be using the android.provider.Settings and the android support library . 您应该使用android.provider.Settingsandroid support library It is working find on my GS3 which is v4.2. 在我的GS3 v4.2上可以找到它。

Also looking at the LogCat it looks like you have two instances of the LocalSettings running some how. 还要查看LogCat,就好像您有两个LocalSettings实例如何运行。 Make sure you are not instantiating two of the same Activity at the same time accessing the phone settings. 确保您没有同时访问电话设置来实例化两个相同的活动。 Strict Mode seems to not like that you have two instances open when it is expecting 1 so it kills your program. 严格模式似乎不希望当您期望1时打开两个实例,因此它会杀死您的程序。 I know this might not answer your question but it will probably point you in the right direction. 我知道这可能无法回答您的问题,但可能会为您指明正确的方向。

StrictMode$InstanceCountViolation: class LocationSettingsActivity; instances=2; limit=1

See Also: 也可以看看:

Android Activity Lifecycle Android活动生命周期

Android Strict Mode Android严格模式

StrictMode Instance Count Violation 违反StrictMode实例计数

Solved, 解决了,

I was facing this problem because i was trying to access internet on main thread which works with version < 2.3.6 but this is not supported by higher versions, as they Generate Network on Main thread Exception. 我遇到了这个问题,因为我试图在主线程上访问Internet,该Internet与版本<2.3.6兼容,但是更高版本不支持此功能,因为它们在主线程异常上生成网络。 For this It is always recommended to use Asynctask while using Internet from Your app. 为此,始终建议您在通过应用程序使用Internet时使用Asynctask。 Now it works perfectly well with me. 现在,它对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “设置 &gt; 连接的设备 &gt; 连接首选项”页面的意图 - Intent for “Settings > Connected Devices > Connection Preferences” page 摘要:使用Camera Intent拍摄照片并以正确的方向显示照片(希望所有设备都可以使用) - Summary: Take a picture utilizing Camera Intent and display the photo with correct orientation (works on hopefully all devices) Android bug:裁剪意图适用于某些设备 - Android bug: Crop intent works on some devices 如何在android 4.0中启动wifi AP设置意图? - how to start the wifi AP settings intent in android 4.0? 仅限Android 4.0设备上的Phonegap getCurrentPosition超时 - Phonegap getCurrentPosition timeout on Android 4.0 devices only 所有4.0设备都包含Holo主题吗? - Is Holo theme included in all 4.0 devices? 意向过滤器仅工作一次 - Intent filter works only once ACTION_LOCATION_SOURCE_SETTINGS 仅开启 GPS 但在 Android 中仍未授予权限 10 - ACTION_LOCATION_SOURCE_SETTINGS only turn on GPS but permission is still not granted in Android 10 Intent Extras仅在某些设备上有效 - Intent extras only work on some devices Camera Intent仅在某些设备上返回null - Camera Intent return null only on some devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM