发布《学习计划》android应用

本应用具有如下功能:

新增学习计划,设置计划开始时间,结束时间。一起到达计划开始时间,系统会自动提醒。

        批量删除学习计划

        设置提醒方式,振动或声音或闪亮等



原理:

     1 .在新加计划时,设置提醒时间,到时后转到AlarmBroadReceiver类,核心代码如下:

Intent intent = new Intent(context, AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putString("info", planEntity.getContent());
bundle.putInt("id", planEntity.getId());
intent.putExtras(bundle);


//PendingIntent这个类用于处理即将发生的事情
通过getBroadcast第二个参数区分闹钟,将查询得到的ID值作为第二个参数。
PendingIntent pIntent = PendingIntent.getBroadcast(context, planEntity.getId(), intent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);


Date selectedDate = DateUtils.parseString(planEntity.getStartDate(), "yyyy-MM-dd HH:mm");


am.set(AlarmManager.RTC_WAKEUP, selectedDate.getTime(), pIntent);


   2.并将计划id和提醒时间保存到数据库,为了在开机重启,重新设置提醒

BootBroadcastReceiver

public class BootBroadcastReceiver extends BroadcastReceiver {
public static final LogContext LCTX = PlanLogContext.ROOT.lctx(BootBroadcastReceiver.class);
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        IPlanDao planDao=DaoFactory.getInstance().getDaoInstance(PlanDaoImpl.class);
        List<AlarmEntity> result =planDao.queryAlarmRecords();
        if(CollectionUtils.isEmpty(result)){
        return;
        }
       
        for(AlarmEntity alarmEntity : result){
       
        PlanEntity planEntity=planDao.queryPlanDataById(alarmEntity.getPlanId());
        //重新记录闹钟
        AlarmHelper.recordAlarm(context, planEntity);
        LCTX.d("rerecord alarm . id = " + alarmEntity.getPlanId() +",alarmTime = "+ new Date(alarmEntity.getAlarmTime()));
       
        }
        }
    }


}



3.  到提醒时间后,弹出notifictaion 信息

AlarmReceiver  


ic void onReceive(Context context, Intent intent) {
// 获取传递的信息
Bundle bundle = intent.getExtras();
String info = bundle.getString("info");
int planId = bundle.getInt("id");
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
int icon = R.drawable.icon; // 通知图标
CharSequence tickerText = info; // 状态栏显示的通知文本提示
long when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示
// 用上面的属性初始化Nofification
Notification notification = new Notification(icon, tickerText, when);


String selectedAlarmType = PreferencesUtils.getStringPreference(context, "config", "alarmType", "");
if (selectedAlarmType.equals(context.getString(R.string.alarmTye_sound))) {
//notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 
}
else if (selectedAlarmType.equals(context.getString(R.string.alarmTye_vibrate))) {
//notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300}; 
notification.vibrate = vibrate; 


}
else if (selectedAlarmType.equals(context.getString(R.string.alarmTye_light))) {
//notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.ledARGB = 0xff00ff00; 
notification.ledOnMS = 300; 
notification.ledOffMS = 1000; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
}else{
notification.defaults = Notification.DEFAULT_SOUND; 
}


// 设置提示框的状态
notification.flags = Notification.FLAG_AUTO_CANCEL; // //在通知栏上点击此通知后自动清除此通知
Intent intentTarget = new Intent(context, PlanMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, planId, intentTarget,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "计划开始提醒" + planId, info, contentIntent);
notiManager.notify(planId, notification);


DaoFactory.getInstance().getDaoInstance(PlanDaoImpl.class).deleteAlarmRecord(planId);




界面:




应用下载地址:http://download.csdn.net/detail/pengchua/4312844

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值