-1

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();
    }
}
}
DToon12
  • 1
  • 5

1 Answers1

0

Use this way

find network enable

    public static boolean isNetworkAvailable(Context mContext) {
    ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {

                    return true;
                }
            }
        }
    }
    return false;
}

find aeroplane mode enable

private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
       Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}

add this permission

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Ramkumar.M
  • 551
  • 6
  • 19
  • I have added this to my activity, how would I get a message displayed so that if the device is on aeroplane mode a massage would pop up telling the user the device is on aeroplane mode? – DToon12 Dec 11 '17 at 12:33