Search code examples
androidandroid-pendingintentairplane-mode

Using a PendingIntent to toggle airplane mode


I am working on an app for Android, for which I would like to be able to toggle Airplane Mode using a PendingIntent. Is this possible? If so, how would I do it? Any help would be greatly appreciated!


Solution

  • AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    boolean isEnabled = Settings.System.putInt(context.getContentResolver(), 
    Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", !isEnabled);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(pi);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 20000, 20000, pi);