10月份买了HTC Desire,但是总担心耗电问题,android好友碰面聊得最多的估计就是手机耗电怎么样,看看论坛里面的ROM,都写着稳定、省电......
ROM刷了不低于8个了,也没发现特别省电的,也懒得折腾了,为了省电,每天晚上睡觉前开启飞行模式,早上起来再切换回去,但是老这样也很麻烦,索性写了个定时切换飞行模式的小程序。大体功能如下:
1、指定开启飞行模式的时间
2、指定关闭飞行模式的时间
3、可以开启定时切换,也可以关闭定时切换
分析了一下,发现有这样几个难点:
1、当然是开启飞行模式以及如何关闭飞行模式
2、如何在指定的时间开启飞行模式以及关闭飞行模式
3、重复
开启与关闭飞行模式的代码还是比较简单的,它是采用广播的形式:
try {
ContentResolver cr = context.getContentResolver();
if( state && System.getString(cr,System.AIRPLANE_MODE_ON).equals("0") ){
System.putString(cr,System.AIRPLANE_MODE_ON, "1");
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", true);
context.sendBroadcast(intent);
}else if( !state && System.getString(cr,System.AIRPLANE_MODE_ON).equals("1") ){
System.putString(cr,System.AIRPLANE_MODE_ON, "0");
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", false);
context.sendBroadcast(intent);
}
} catch (Exception e) {
// e.printStackTrace();
Toast.makeText(context, "设置飞行模式状态失败", Toast.LENGTH_SHORT).show();
}
通过设置System.AIRPLANE_MODE_ON值为0或者为1,然后通过intent来发布广播。
第二点,就需要用到闹钟了,闹钟换句话就是一个定时器,只是闹钟的时间到了之后,播放闹钟铃声。在这里我们可以设置两个闹钟,在闹钟的时间到了之后分别开启飞行模式和关闭飞行模式,关于第三点重复也是在闹钟中设置的,关键代码如下:
//设置日历的时间,主要是让日历的年月日和当前同步
calendar.setTimeInMillis(java.lang.System.currentTimeMillis());
//设置日历的小时和分钟
calendar.set(Calendar.HOUR_OF_DAY, sh);
calendar.set(Calendar.MINUTE, sm);
//将秒和毫秒设置为0
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
//建立Intent和PendingIntent来调用闹钟管理器
Intent startIntent = new Intent(ALARM_ACTION_START);
startIntent.putExtra("startState", 1);
PendingIntent startPendingIntent = PendingIntent.getBroadcast(TimerAirPlaneMode.this, 0, startIntent, 0);
//获取闹钟管理器
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//设置开始时间对应的闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, startPendingIntent);
上面的代码就是设置闹钟的,最后一行代码就是设置重复闹钟的。
这里还有一个问题是需要解决的,那就是如何同时设置多个闹钟,这个花了我一点时间,最后发现是通过PendingIntent.getBroadcast(TimerAirPlaneMode.this, 1, endIntent, 0);的第二个参数来定义一个新的闹钟。
然后就是关闭闹钟了:
Intent intent = new Intent(ALARM_ACTION_START);
PendingIntent pendingIntent = PendingIntent.getBroadcast(TimerAirPlaneMode.this, 0, intent, 0);
//获取闹钟管理器
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//取消开始和结束闹钟
alarmManager.cancel(pendingIntent);
关闭闹钟首先要得到生成闹钟的PendingIntent,这个时候需要注意多个闹钟需要独立取消。
到这里关键代码基本上没有了,剩下的就是数据存储问题了,如何在下次启动程序的时候,自动读取上一次的数据,避免重复设置,这里需要用到SharedPreferences,用它来存储数据以及读取数据。
Log.d(TAG, "set the time:"+h+":"+m);
SharedPreferences preferences = getSharedPreferences("TimerAirPlaneMode", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("flag", flag?1:0);
if( flag ){
editor.putInt("startHour", h);
editor.putInt("startMinute", m);
} else {
editor.putInt("endHour", h);
editor.putInt("endMinute", m);
}
editor.commit();//这步很关键
SharedPreferences preferences = getSharedPreferences("TimerAirPlaneMode",MODE_PRIVATE);
int sh = preferences.getInt("startHour", -1);
int sm = preferences.getInt("startMinute", -1);
这里要注意,最后的提交commit很关键,要不然数据是不会写入到文件中去的,上面的第二部分代码是从SharedPreferences中读取数据。
下面是程序的运行效果: