Это может быть трудно дать MCVE, потому что это требует, чтобы приложение было внесено в белый список с разрешением WRITE_SECURE_SETTINGS
на рутированном устройстве, но здесь идет.
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestingStrangeBehavior";
private boolean locationOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
int location = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE, LOCATION_MODE_OFF);
locationOn = location != LOCATION_MODE_OFF;
findViewById(R.id.toggle_location_button).setOnClickListener(view -> {
locationOn = !locationOn;
Log.d(TAG, "Turning location " + (locationOn?"on":"off"));
Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE,
locationOn ? LOCATION_MODE_SENSORS_ONLY : LOCATION_MODE_OFF);
});
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause called");
}
}
Вот схема:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.TestActivity">
<Button
android:id="@+id/toggle_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Location"/>
</android.support.constraint.ConstraintLayout>
Вывод, который я получаю от нажатия кнопки «Переключить местоположение»:
D / TestingStrangeBehavior: включение местоположения
D / TestingStrangeBehavior: onPause вызывается
D / TestingStrangeBehavior: onResume вызывается
D / TestingStrangeBehavior: отключение местоположения
D / TestingStrangeBehavior: onPause вызывается
D / TestingStrangeBehavior: вызвано: 10Resume*
Как видите, каждый раз, когда я включаю / выключаю местоположение, действие приостанавливается и возобновляется.
Но если я putInt
на другом поле, например ACCESSIBILITY_ENABLED
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
int location = Settings.Secure.getInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0);
locationOn = location != 0;
findViewById(R.id.toggle_location_button).setOnClickListener(view -> {
locationOn = !locationOn;
Log.d(TAG, "Turning location " + (locationOn?"on":"off"));
Settings.Secure.putInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED,
locationOn ? 1 : 0);
});
}
Не приостанавливает и не возобновляет активность.
Есть ли причина для этого?Зачем переключать местоположение, приостанавливать и возобновлять активность?Это ошибка в Android?