在特定时间设置airplanemode

时间:2012-06-21 13:12:11

标签: android airplane

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;          
}

}

我想在设定的时间更改飞机模式,这是我从时间选择器获得的。

然后我将时间戳的时间设置为女巫的日历,我得到了alarmmanager.set方法的时间,但它没有做任何事情。

我在互联网上观看,但我没有找到任何东西。我在stackoverflow上发现了这个帖子,但没有回答 (对不起,我的英语不好) 谢谢你的回答

1 个答案:

答案 0 :(得分:0)

我在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();
    }
};

我认为您需要执行异步任务,因为您可以在此处或在我链接的帖子中看到。

编辑:我在答案中也找到How can one detect airplane mode on Android?知道它是否被激活的方式(做某事或不做;)

/**
* 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;

}

最后我发现了这个http://dustinbreese.blogspot.com.es/2009/04/andoid-controlling-airplane-mode.html,我认为你需要它!