I have an application which uses a Google PlacePicker and Google StreetView. I have to test the application with the device in aeroplane mode.
I want the application to check when it reaches this activity if aeroplane mode is active, if it is not active I would like a Toast
displayed
Below is the code for an Activity an activity in which there are 2 buttons, one of the buttons loads the PlacePicker and the other loads another activity which contains the StreetView.
I have seen this How can one detect airplane mode on Android? answer and that is where I got the code I have attempted in my answer but it will not work
Can someone please tell me how to get this working
public class PlacePickerActivity extends AppCompatActivity {
int PLACE_PICKER_REQUEST = 1;
TextView tvPlace;
private Button buttonStepsActivity;
private Button buttonStreet;
private boolean isAirplaneModeOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker);
tvPlace = (TextView)findViewById(R.id.tvPlace);
//StreetView
buttonStreet = (Button) findViewById(R.id.buttonStreet);
if(isAirplaneModeOn) {
Toast.makeText(this,"Aeroplane mode active",Toast.LENGTH_LONG).show();
}
}
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
public void goPlacePicker(View v) {
// Construct an intent for the place picker
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(this);
// Start the intent by requesting a result,
// identified by a request code.
startActivityForResult(intentBuilder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("You have chosen: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}