Check if gps is on In Kitkat (4.4)

ChangUZ

Below kitkat, I get if gps is on with providers list

String providers = Secure.getString(context.getContentResolver(), Secure.LOCATION_PROVIDERS_ALLOWED);
if (TextUtils.isEmpty(providers)) {
    return false;
}
return providers.contains(LocationManager.GPS_PROVIDER);

But in kitkat, Secure.LOCATION_PROVIDERS_ALLOWED is deprecated.

Javaodc say "use Secure.LOCATION_MODE", Location modes are belows...

Secure.LOCATION_MODE_OFF
Secure.LOCATION_MODE_SENSORS_ONLY
Secure.LOCATION_MODE_BATTERY_SAVING
Secure.LOCATION_MODE_HIGH_ACCURACY

But i do not know exactly below code is correct. (I do not have kitkat devices)

int locationMode = Secure.getInt(context.getContentResolver(), Secure.LOCATION_MODE);
boolean isGpsOn = locationMode != Secure.LOCATION_MODE_OFF;

If incorrect, Answer correct code please.

EDIT

I know LocationManager. But It require ACCESS_FINE_LOCATION permission. I do not need to "Access" location.

LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
boolean isGpsOn = manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) 
ChangUZ

Self Answer

@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public static boolean isGpsEnabled(Context context) {

    if (PackageUtil.checkPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        String providers = Secure.getString(context.getContentResolver(),
                Secure.LOCATION_PROVIDERS_ALLOWED);
        if (TextUtils.isEmpty(providers)) {
            return false;
        }
        return providers.contains(LocationManager.GPS_PROVIDER);
    } else {
        final int locationMode;
        try {
            locationMode = Secure.getInt(context.getContentResolver(),
                    Secure.LOCATION_MODE);
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        switch (locationMode) {

        case Secure.LOCATION_MODE_HIGH_ACCURACY:
        case Secure.LOCATION_MODE_SENSORS_ONLY:
            return true;
        case Secure.LOCATION_MODE_BATTERY_SAVING:
        case Secure.LOCATION_MODE_OFF:
        default:
            return false;
        }
    }
}

And Add this method in your packageutils class :

public static class PackageUtil {

    static boolean checkPermission(Context context, String accessFineLocation) {

        int res = context.checkCallingOrSelfPermission(accessFineLocation);
        return (res == PackageManager.PERMISSION_GRANTED);

    }

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Check if there is 4 letters in a message

分類Dev

check if key exists firebase 4 and swift 4?

分類Dev

mp4parserがGPS位置を取得

分類Dev

Check if user is already authenticated in IdentityServer 4

分類Dev

How to check the correct answer JavaScript (4 BUTTONS)

分類Dev

Check ontology consistency & satisifiability with OWL API 4

分類Dev

Problems with check if GPS, and bluetooth is activated

分類Dev

Check GPS state (on/off)in cordova android

分類Dev

Nexus 4でGPSを使用するにはどうすればよいですか?

分類Dev

How to check if an index exist in neo4j cypher

分類Dev

How to check if an index exist in neo4j cypher

分類Dev

How To Check Browser close Condition for Angular 4 Application?

分類Dev

How to check stateChanged in a dynamic checkbox list in PyQt4

分類Dev

How to check if log4j2 has been configured or not

分類Dev

Rails 4 collection_check_boxes、has_manyから

分類Dev

Neo4j: Check condition between all consecutive relations in path

分類Dev

Neo4j: Check condition between all consecutive relations in path

分類Dev

Angular 2/4 : How to check length of input's value using directive?

分類Dev

CORS issue in codeigniter 4: Response to preflight request doesn't pass access control check

分類Dev

How to check existing data with jquery validation library with codeigniter 4 when csrf is set to auto?

分類Dev

collection_check_boxesにhtmlを追加する方法(Rails 4)

分類Dev

QTouchEvent всегда вызывается 4 раза при любом взаимодействии

分類Dev

Numpy multiply (2, 4) and (4, 4) without broadcasting

分類Dev

Log4j2 and Spring 4

分類Dev

Log4j2 and Spring 4

分類Dev

CAGradientLayer with 4 Colors in 4 Different Corners

分類Dev

Spring 4 + Hibernate 4:sessionFactoryがnull

分類Dev

Gmaps4rails-Rails 4

分類Dev

jdbc4 CommunicationsException

Related 関連記事

  1. 1

    Check if there is 4 letters in a message

  2. 2

    check if key exists firebase 4 and swift 4?

  3. 3

    mp4parserがGPS位置を取得

  4. 4

    Check if user is already authenticated in IdentityServer 4

  5. 5

    How to check the correct answer JavaScript (4 BUTTONS)

  6. 6

    Check ontology consistency & satisifiability with OWL API 4

  7. 7

    Problems with check if GPS, and bluetooth is activated

  8. 8

    Check GPS state (on/off)in cordova android

  9. 9

    Nexus 4でGPSを使用するにはどうすればよいですか?

  10. 10

    How to check if an index exist in neo4j cypher

  11. 11

    How to check if an index exist in neo4j cypher

  12. 12

    How To Check Browser close Condition for Angular 4 Application?

  13. 13

    How to check stateChanged in a dynamic checkbox list in PyQt4

  14. 14

    How to check if log4j2 has been configured or not

  15. 15

    Rails 4 collection_check_boxes、has_manyから

  16. 16

    Neo4j: Check condition between all consecutive relations in path

  17. 17

    Neo4j: Check condition between all consecutive relations in path

  18. 18

    Angular 2/4 : How to check length of input's value using directive?

  19. 19

    CORS issue in codeigniter 4: Response to preflight request doesn't pass access control check

  20. 20

    How to check existing data with jquery validation library with codeigniter 4 when csrf is set to auto?

  21. 21

    collection_check_boxesにhtmlを追加する方法(Rails 4)

  22. 22

    QTouchEvent всегда вызывается 4 раза при любом взаимодействии

  23. 23

    Numpy multiply (2, 4) and (4, 4) without broadcasting

  24. 24

    Log4j2 and Spring 4

  25. 25

    Log4j2 and Spring 4

  26. 26

    CAGradientLayer with 4 Colors in 4 Different Corners

  27. 27

    Spring 4 + Hibernate 4:sessionFactoryがnull

  28. 28

    Gmaps4rails-Rails 4

  29. 29

    jdbc4 CommunicationsException

ホットタグ

アーカイブ