0
public class AirPlaneModeActivity extends Activity {

Button b;
TimePicker tp;
Calendar cal;
AlarmManager am ;
PendingIntent pi;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b = (Button)findViewById(R.id.button1);
    tp = (TimePicker)findViewById(R.id.timePicker1);
    cal = Calendar.getInstance(Locale.getDefault());
    am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    tp.setIs24HourView(true);        

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            cal.set(Calendar.HOUR_OF_DAY,tp.getCurrentHour());
            cal.set(Calendar.MINUTE,tp.getCurrentMinute());
            cal.set(Calendar.SECOND,0);
        }
    }); 

    pi = PendingIntent.getBroadcast(this, 0, setAPM(), 0);       
    am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pi);        

}
public Intent setAPM(){
    boolean isEnabled = Settings.System.getInt(
              getContentResolver(), 
              Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    // toggle airplane mode
    Settings.System.putInt(getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

    // Post an intent to reload
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", !isEnabled);
    return intent;          
}

}

I want to change the airplanemode at the setted time, that I get from a timepicker.

Then I set the time from the timepicker into a calender from witch I get the time for the alarmmanager.set Method but it doesn't do anything.

I watch all over the internet but I didn't found anything. I found this post on stackoverflow but without answer (Sorry for my bad english) Thanks for your answers

Ferran Buireu
  • 14,305
  • 5
  • 20
  • 41

1 Answers1

0

I found this in My code toggles airplane mode continuously

/** Code snippet in AirplaneModeService*/
@Override
public void onCreate() {
    airplaneModeToggler = new AirplaneModeToggler(this);
    Thread mThread = new Thread(null, airplaneModeToggleTask, "AirplaneModeToggleTask");
    mThread.start();
}

private Runnable airplaneModeToggleTask = new Runnable() {
    @Override
    public void run() {
        airplaneModeToggler.toggle(null);
        AirplaneModeService.this.stopSelf();
    }
};

I think you need to do an asynchronous task to d that as you can see up here or in the post I linked you.

Edit: I also found here How can one detect airplane mode on Android? in the answer the way to know if it is activated or not (to do something or not ;) )

/**
* Gets the state of Airplane Mode.
* 
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {

   return Settings.System.getInt(context.getContentResolver(),
           Settings.System.AIRPLANE_MODE_ON, 0) != 0;

}

And finally I found this http://dustinbreese.blogspot.com.es/2009/04/andoid-controlling-airplane-mode.html where I think there is all you need!

Community
  • 1
  • 1
Miquel Coll
  • 729
  • 12
  • 43