簡體   English   中英

位置來源設置意圖僅適用於<4.0的所有設備

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

您好,下面是我的代碼,用於檢查gps設置,該代碼可在所有Android 2.3.6及更低版本的設備上完美運行,但不適用於ICS和Jellybean。 當我檢查設備上是否啟用了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();
        }

這就是我在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)

首先,請確保您沒有導入錯誤的設置庫。 您應該使用android.provider.Settingsandroid support library 在我的GS3 v4.2上可以找到它。

還要查看LogCat,就好像您有兩個LocalSettings實例如何運行。 確保您沒有同時訪問電話設置來實例化兩個相同的活動。 嚴格模式似乎不希望當您期望1時打開兩個實例,因此它會殺死您的程序。 我知道這可能無法回答您的問題,但可能會為您指明正確的方向。

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

也可以看看:

Android活動生命周期

Android嚴格模式

違反StrictMode實例計數

解決了,

我遇到了這個問題,因為我試圖在主線程上訪問Internet,該Internet與版本<2.3.6兼容,但是更高版本不支持此功能,因為它們在主線程異常上生成網絡。 為此,始終建議您在通過應用程序使用Internet時使用Asynctask。 現在,它對我來說效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM