关于android闹钟,设置定时提醒的一点心得
2014-04-03 15:26
387 查看
首先在设置提醒之前你需要一个入口,比如说onclick事件中,在此不做赘述。
android中使用闹钟进行提醒其实非常简单,你只需要告知系统你想在什么时候被提醒,然后需要一个闹钟的广播接收器,当到你设置的时间时,系统会给你发送一条广播,当你接收到广播后你就可以做一些操作,比如启动你的app,或者跳转到你app中的任何一个界面。废话不多少,直接上代码。
到这里闹钟提醒的功能就基本完成了。有问题可以留言。
android中使用闹钟进行提醒其实非常简单,你只需要告知系统你想在什么时候被提醒,然后需要一个闹钟的广播接收器,当到你设置的时间时,系统会给你发送一条广播,当你接收到广播后你就可以做一些操作,比如启动你的app,或者跳转到你app中的任何一个界面。废话不多少,直接上代码。
01 | //发送闹钟请求 |
02 | Intent intent = new Intent(mContext, AlarmReceiver. class ); |
03 | intent.setAction( "something" ); |
04 | intent.setType( "something" ); |
05 | intent.setData(Uri.EMPTY); |
06 | intent.addCategory(“something”); |
07 | intent.setClass(context, AlarmReceiver. class ); |
08 | // 以上给intent设置的四个属性是用来区分你发给系统的闹钟请求的,当你想取消掉之前发的闹钟请求,这四个属性,必须严格相等,所以你需要一些比较独特的属性,比如服务器返回给你的json中某些特定字段。 |
09 | //当然intent中也可以放一些你要传递的消息。 |
10 | PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmCount, intent, 0 ); |
11 | //alarmCount是你需要记录的闹钟数量,必须保证你所发的alarmCount不能相同,最后一个参数填0就可以。 |
12 | AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
13 | am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); |
14 | //这样闹钟的请求就发送出去了。time是你要被提醒的时间,单位毫秒,注意不是时间差。第一个参数提醒的需求用我给出的就可以,感兴趣的朋友,可以去google一下,这方面的资料非常多,一共有种,看一下就知道区别了。 |
15 | //取消闹钟请求 |
16 | Intent intent = new Intent(mContext, AlarmReceiver. class ); |
17 | intent.setAction( "something" ); |
18 | intent.setType(something); |
19 | intent.setData(Uri.EMPTY); |
20 | intent.addCategory(something); |
21 | intent.setClass(context, AlarmReceiver. class ); |
22 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alarmCount, intent, 0 ); |
23 | //alarmCount对应到你设定时的alarmCount, |
24 | AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
25 | am.cancel(pendingIntent); |
26 | //接着,你需要一个广播接收的类: |
27 | public class AlarmReceiver extends BroadcastReceiver{ |
28 |
29 | private NotificationManager manager; |
30 |
31 | @Override |
32 | public void onReceive(Context context, Intent intent) { |
33 | manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); |
34 | //例如这个id就是你传过来的 |
35 | String id = intent.getStringExtra( "id" ); |
36 | //MainActivity是你点击通知时想要跳转的Activity |
37 | Intent playIntent = new Intent(context, MainActivity. class ); |
38 | playIntent.putExtra( "id" , id); |
39 | PendingIntent pendingIntent = PendingIntent.getActivity(context, 1 , playIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
40 | NotificationCompat.Builder builder = new NotificationCompat.Builder(context); |
41 | builder.setContentTitle( "title" ).setContentText( "提醒内容" ).setSmallIcon(R.drawable.app_icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel( true ).setSubText( "二级text" ); |
42 | manager.notify( 1 , builder.build()); |
43 | } |
44 | } |
相关文章推荐
- android调用.net的webservice的例子获取手机的归属地
- android:descendantFocusability用法简析
- Android adt22.6.1->22.6.2更新后报Exception raised during rendering: Empty region!
- [Android学习笔记]jackson库的使用
- [Android学习笔记]some tips
- AIDL-Android接口描述语言实现跨进程通讯
- Android -- service 利用广播调用服务的方法
- Android -- service 利用广播调用服务的方法
- android圆角View实现及不同版本这间的兼容
- android中共享全局数据的方法(static导致的内存泄漏问题,但其实并未解决)
- android_API拦截技术与注入技术
- android_Service与Binder的关系
- android ContentProvider
- android_Surface,SurfaceView,Canvas的关系
- IntelliJ IDEA & Genymotion--接触新的开发工具
- android_OOM分析:缩小资源,及时释放
- android_Android操作系统的内存回收机制
- android_String对象
- Android 仿美团网,大众点评购买框悬浮效果之修改版
- Android 对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果