AlarmService---让进城在后台一直…
来源:互联网 发布:宁夏干部网络培训官网 编辑:程序博客网 时间:2024/06/11 09:41
连接:http://blog.csdn.net/wanglang3081/article/details/7898674
实验2:使用AlarmService:
2.1通过AlarmService每个5秒发送一个广播,setRepeating时的类型为AlarmManager.ELAPSED_REALTIME。
AlarmManager am =(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,firstTime,
5
*
1000
,sender);
拔掉USB线,按下电源键,过一段时间再次打开屏幕,发现定时器没有继续计数。
2.2setRepeating是的类型设置为AlarmManager.ELAPSED_REALTIME_WAKEUP
AlarmManager am =(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,
5
*
1000
,sender);
拔掉USB线,按下电源键,过一点时间再次打开屏幕,发现定时器一直在计数。
如此看来,使用WAKEUP才能保证自己想要的定时器一直工作,但是肯定会引起耗电量的增加
AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作
AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.
下面的代码详细的介绍了两种定时方式的使用:
-
-
Intent intent =new Intent(Main. this,alarmreceiver. class); -
intent.setAction("short"); -
PendingIntent sender= -
PendingIntent.getBroadcast(Main.this, 0, intent, 0); -
-
//设定一个五秒后的时间 -
Calendar calendar=Calendar.getInstance(); -
calendar.setTimeInMillis(System.currentTimeMillis()); -
calendar.add(Calendar.SECOND, 5); -
-
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); -
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); -
//或者以下面方式简化 -
//alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender); -
-
Toast.makeText(Main.this, "五秒后alarm开启", Toast.LENGTH_LONG).show();
-
-
Intent intent = Intent(Main. this,alarmreceiver. class); -
intent.setAction( -
PendingIntent sender= -
PendingIntent.getBroadcast(Main. 0, intent, 0); -
-
-
Calendar calendar=Calendar.getInstance(); -
calendar.setTimeInMillis(System.currentTimeMillis()); -
calendar.add(Calendar.SECOND, -
-
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); -
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); -
-
System.currentTimeMillis()+5*1000, sender); -
-
Toast.makeText(Main. "五秒后alarm开启", Toast.LENGTH_LONG).show();
//操作:发送一个广播,广播接收后Toast提示定时操作完成 Intent intent =new Intent(Main.this, alarmreceiver.class); intent.setAction("short"); PendingIntent sender= PendingIntent.getBroadcast(Main.this, 0, intent, 0); //设定一个五秒后的时间 Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 5); AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); //或者以下面方式简化 //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender); Toast.makeText(Main.this, "五秒后alarm开启", Toast.LENGTH_LONG).show();
//注意:receiver记得在manifest.xml注册
-
public static class alarmreceiver extendsBroadcastReceiver{ -
-
@Override -
public void onReceive(Context context, Intent intent) { -
// TODO Auto-generated method stub -
if(intent.getAction().equals("short")){ -
Toast.makeText(context, "short alarm" ,Toast.LENGTH_LONG).show(); -
}else{ -
Toast.makeText(context, "repeating alarm" , -
Toast.LENGTH_LONG).show(); -
} -
} -
}
-
static class alarmreceiver extendsBroadcastReceiver{ -
-
-
void onReceive(Context context, Intent intent) { -
TODO Auto-generated method stub -
-
Toast.makeText(context, alarm" ,Toast.LENGTH_LONG).show(); -
} -
Toast.makeText(context, alarm" , -
Toast.LENGTH_LONG).show(); -
} -
} -
}
public static class alarmreceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals("short")){ Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(context, "repeating alarm", Toast.LENGTH_LONG).show(); } } }
(2)周期性的执行某项操作
-
Intent intent =new Intent(Main. this,alarmreceiver. class); -
intent.setAction("repeating"); -
PendingIntent sender=PendingIntent -
.getBroadcast(Main.this, 0, intent, 0); -
-
-
//开始时间 -
long firstime=SystemClock.elapsedRealtime(); -
-
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); -
- //5秒一个周期,不停的发送广播
-
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP -
, firstime, 5*1000, sender);
-
Intent intent = Intent(Main. this,alarmreceiver. class); -
intent.setAction( -
PendingIntent sender=PendingIntent -
.getBroadcast(Main. 0, intent, 0); -
-
-
-
firstime=SystemClock.elapsedRealtime(); -
-
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); -
- //5秒一个周期,不停的发送广播
-
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP -
, firstime, sender);
Intent intent =new Intent(Main.this, alarmreceiver.class); intent.setAction("repeating"); PendingIntent sender=PendingIntent .getBroadcast(Main.this, 0, intent, 0); //开始时间 long firstime=SystemClock.elapsedRealtime(); AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); //5秒一个周期,不停的发送广播 am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP , firstime, 5*1000, sender);
以后开始周期性的执行task;
AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)
-
Intent intent =new Intent(Main. this,alarmreceiver. class); -
intent.setAction("repeating"); -
PendingIntent sender=PendingIntent -
.getBroadcast(Main.this, 0, intent, 0); -
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); -
alarm.cancel(sender);
-
Intent intent = Intent(Main. this,alarmreceiver. class); -
intent.setAction( -
PendingIntent sender=PendingIntent -
.getBroadcast(Main. 0, intent, 0); -
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); -
alarm.cancel(sender);
- AlarmService---让进城在后台一直…
- AlarmService---让进城在后台一直…
- 让iOS程序一直在后台运行
- 调用系统声音, 震动, 让程序一直在后台运行
- 如何让android的service一直在后台运行?
- iOS让你的app一直在后台活着(运行)
- 如何让android的service一直在后台运行
- linus 让一个程序在后台一直运行
- [转] 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
- Android返回键等于home键 让运用一直在后台运行
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
- 让一个Android应用一直运行在后台,不容易被杀死
- 简单优雅的让你的app一直运行在后台
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
- 让命令在后台执行
- 保持service一直在后台运行
- java并发集合的优缺点
- Java并发小结
- [Android实例] 调用相册和拍…
- Notification清除所有的消息通知
- AlarmService---让进城在后台一直…
- AlarmService---让进城在后台一直…
- 关于Android手机系统分辨率和版本…
- About size_t and ptrdiff_t
- java web 流媒体服务
- android中不同手机分辨率适配问题
- Gson解析Json数据
- 判断软键盘,显示或隐藏
- 【Android】屏幕翻转(screen&nbsp…
- [Android]TextView跑马灯效果