|
1 | 1 | package amotz.example.com.mocklocationfordeveloper;
|
2 | 2 |
|
| 3 | +import android.app.AppOpsManager; |
3 | 4 | import android.content.Context;
|
4 | 5 | import android.content.DialogInterface;
|
5 | 6 | import android.content.Intent;
|
6 | 7 | import android.content.pm.PackageManager;
|
7 | 8 | import android.location.Location;
|
| 9 | +import android.os.Build; |
8 | 10 | import android.provider.Settings;
|
9 | 11 | import android.support.annotation.NonNull;
|
10 | 12 | import android.support.annotation.Nullable;
|
@@ -44,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
|
44 | 46 | super.onCreate(savedInstanceState);
|
45 | 47 | setContentView(R.layout.activity_main);
|
46 | 48 |
|
47 |
| - if (!isMockSettingsON(this)) { |
| 49 | + if (!isMockSettingsON()) { |
48 | 50 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
49 | 51 | builder.setMessage("In order to use this app you must enable mock location do you want to enable it now?").setTitle("Mock location is not enable");
|
50 | 52 | builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
|
@@ -86,12 +88,28 @@ protected void onStop() {
|
86 | 88 | super.onStop();
|
87 | 89 | }
|
88 | 90 |
|
89 |
| - private boolean isMockSettingsON(Context context) { |
90 |
| - if (android.os.Build.VERSION.SDK_INT < 18) { |
91 |
| - String st = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION); |
92 |
| - return st.equals("1"); |
| 91 | + private boolean isMockSettingsON() { |
| 92 | + boolean isMockLocation = false; |
| 93 | + try |
| 94 | + { |
| 95 | + //if marshmallow |
| 96 | + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) |
| 97 | + { |
| 98 | + AppOpsManager opsManager = (AppOpsManager) this.getSystemService(Context.APP_OPS_SERVICE); |
| 99 | + isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + // in marshmallow this will always return true |
| 104 | + isMockLocation = !android.provider.Settings.Secure.getString(this.getContentResolver(), "mock_location").equals("0"); |
| 105 | + } |
93 | 106 | }
|
94 |
| - return true; |
| 107 | + catch (Exception e) |
| 108 | + { |
| 109 | + return isMockLocation; |
| 110 | + } |
| 111 | + |
| 112 | + return isMockLocation; |
95 | 113 |
|
96 | 114 | }
|
97 | 115 |
|
|
0 commit comments