android:catation="90",Android中的AlarmManager的使用.htm

var protocol = window.location.protocol;

document.write('

Android中的AlarmManager的使用 - wangxingwu_314的专栏

- 博客频道 - CSDN.NET

var _hmt = _hmt || [];

(function () {

var hm = document.createElement("script");

hm.src = "//hm.baidu.com/hm.js?6bcd52f51e9b3dce32bec4a3997715ac";

var s = document.getElementsByTagName("script")[0];

s.parentNode.insertBefore(hm, s);

})();

var username = "wangxingwu_314";

var _blogger = username;

var blog_address = "http://blog.csdn.net/wangxingwu_314";

var static_host = "http://static.blog.csdn.net";

var currentUserName = "";

聚焦行业最佳实践,BDTC 2016完整议程公布

微信小程序实战项目——点餐系统

程序员11月书讯,评论得书啦

Get IT技能知识库,50个领域一键直达

window.quickReplyflag = true;

var isBole = false;

Android中的AlarmManager的使用

标签:

androidactionservicecalendarclasshtml

40729人阅读

评论(7)

收藏

举报

本文章已收录于:

.embody{

padding:10px 10px 10px;

margin:0 -20px;

border-bottom:solid 1px #ededed;

}

.embody_b{

margin:0 ;

padding:10px 0;

}

.embody .embody_t,.embody .embody_c{

display: inline-block;

margin-right:10px;

}

.embody_t{

font-size: 12px;

color:#999;

}

.embody_c{

font-size: 12px;

}

.embody_c img,.embody_c em{

display: inline-block;

vertical-align: middle;

}

.embody_c img{

width:30px;

height:30px;

}

.embody_c em{

margin: 0 20px 0 10px;

color:#333;

font-style: normal;

}

$(function () {

try

{

var lib = eval("("+$("#lib").attr("value")+")");

var html = "";

if (lib.err == 0) {

$.each(lib.data, function (i) {

var obj = lib.data[i];

//html += ''%20+%20obj.logo%20+%20'' + obj.name + "  ";

html += ' ';

html += ' '%20+%20obj.logo%20+%20'';

html += ' ' + obj.name + '';

html += ' ';

});

if (html != "") {

setTimeout(function () {

$("#lib").html(html);

$("#embody").show();

}, 100);

}

}

} catch (err)

{ }

});

1、AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent。简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent,PendingIntent可以理解为Intent的封装包,简单的说就是在Intent上在加个指定的动作。在使用Intent的时候,我们还需要在执行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的话就是将这个动作包含在内了。

定义一个PendingIntent对象。

PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);

2、AlarmManager的常用方法有三个:

(1)set(int type,long startTime,PendingIntent pi);

该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。

3、三个方法各个参数详悉:

(1)int type: 闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;

(2)long startTime: 闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对 应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于 系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime();如果第一个参数对应的闹钟使用的是绝对时间 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示

为:System.currentTimeMillis()。

(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi: 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提 示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getBroadcast(Context

c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

4.举例说明:定义一个闹钟,5秒钟重复响应。

(1)MainActivity,在onCreate中完成:

  1. //创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”  
  2. Intent intent = new Intent("ELITOR_CLOCK");  
  3. intent.putExtra("msg","你该打酱油了");    
  4.   
  5. //定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。  
  6. //也就是发送了action 为"ELITOR_CLOCK"的intent
  7. PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);    
  8.   
  9. //AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务  
  10. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);    
  11.   
  12. //设置闹钟从当前时间开始,每隔5s执行一次PendingIntent对象pi,注意第一个参数与第二个参数的关系  
  13. // 5秒后通过PendingIntent pi对象发送广播  
  14. am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);  
//创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”

Intent intent = new Intent("ELITOR_CLOCK");

intent.putExtra("msg","你该打酱油了");

//定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。

//也就是发送了action 为"ELITOR_CLOCK"的intent

PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);

//AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

//设置闹钟从当前时间开始,每隔5s执行一次PendingIntent对象pi,注意第一个参数与第二个参数的关系

// 5秒后通过PendingIntent pi对象发送广播

am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);

那么启动MainActivity之后,由于定义了AlarmManager am,并且调用了am.setRepeating(...)函数,则系统每隔5s将会通过pi启动intent发送广播,其action为ELITOR_CLOCK。所以我们需要在Manifest.xml中注册一个receiver,同时自己定义一个广播接收器类。

(2)定义一个广播接收器类MyReceiver,重写onReceive()函数。

  1. public class MyReceiver extends BroadcastReceiver  
  2. {  
  3.   
  4.     @Override  
  5.     public void onReceive(Context context, Intent intent)  
  6.     {  
  7.         // TODO Auto-generated method stub  
  8.         Log.d("MyTag""onclock......................");  
  9.         String msg = intent.getStringExtra("msg");  
  10.         Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();  
  11.     }  
  12.   
  13. }  
public class MyReceiver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

// TODO Auto-generated method stub

Log.d("MyTag", "onclock......................");

String msg = intent.getStringExtra("msg");

Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();

}

}

(3)在Manifest.xml中注册广播接收器:

  1. <receiver android:name=".MyReceiver">  
  2.         <intent-filter>  
  3.             <action android:name="ELITOR_CLOCK" />  
  4.         </intent-filter>  
  5. </receiver>  
<receiver android:name=".MyReceiver">

<intent-filter>

<action android:name="ELITOR_CLOCK" />

</intent-filter>

</receiver>

补充:设置指定的时间启动广播。

import java.util.Calendar;

import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

public class AlarmActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Calendar c=Calendar.getInstance();

c.set(Calendar.YEAR,2011);

c.set(Calendar.MONTH,Calendar.JUNE);//也可以填数字,0-11,一月为0

c.set(Calendar.DAY_OF_MONTH, 28);

c.set(Calendar.HOUR_OF_DAY, 19);

c.set(Calendar.MINUTE, 50);

c.set(Calendar.SECOND, 0);

//设定时间为 2011年6月28日19点50分0秒

//c.set(2011, 05,28, 19,50, 0);

//也可以写在一行里

Intent intent=new Intent(this,AlarmReceiver.class);

PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);

//设置一个PendingIntent对象,发送广播

AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);

//获取AlarmManager对象

am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);

//时间到时,执行PendingIntent,只执行一次

//AlarmManager.RTC_WAKEUP休眠时会运行,如果是AlarmManager.RTC,在休眠时不会运行

//am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 10000, pi);

//如果需要重复执行,使用上面一行的setRepeating方法,倒数第二参数为间隔时间,单位为毫秒

}

}

document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)

11
1
 
 

function btndigga() {

$(".tracking-ad[data-mod='popu_222'] a").click();

}

function btnburya() {

$(".tracking-ad[data-mod='popu_223'] a").click();

}

$(function(){

$.get("/wangxingwu_314/svc/GetSuggestContent/8060312",function(data){

$("#suggest").html(data);

});

});

.blog-ass-articl dd {

color: #369;

width: 99%; /*修改行*/

float: left;

overflow: hidden;

font: normal normal 12px/23px "SimSun";

height: 23px;

margin: 0;

padding: 0 0 0 10px;

margin-right: 30px;

background: url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;

}

参考知识库

更多资料请参考:

猜你在找

csdn.position.showEdu({

sourceType: "blog",

searchType: "detail",

searchKey: "8060312",

username: "",

recordcount: "5",

containerId: "adCollege" //容器DIV的id。

});

$(function () {

setTimeout(function () {

var searchtitletags = 'Android中的AlarmManager的使用' + ',' + $("#tags").html();

searchService({

index: 'blog',

query: searchtitletags,

from: 5,

size: 5,

appendTo: '#res',

url: 'recommend',

his: 2,

client: "blog_cf_enhance",

tmpl: '

#{ title }'

});

}, 500);

});

/*博客内容页下方Banner-728*90,创建于2014-7-3*/

var cpro_id = "u1607657";

查看评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

var fileName = '8060312';

var commentscount = 7;

var islock = false

快速回复

TOP

$(function ()

{

$("#ad_frm_0").height("90px");

setTimeout(function(){

$("#ad_frm_2").height("200px");

},1000);

});

.tag_list

{

background: none repeat scroll 0 0 #FFFFFF;

border: 1px solid #D7CBC1;

color: #000000;

font-size: 12px;

line-height: 20px;

list-style: none outside none;

margin: 10px 2% 0 1%;

padding: 1px;

}

.tag_list h5

{

background: none repeat scroll 0 0 #E0DBD3;

color: #47381C;

font-size: 12px;

height: 24px;

line-height: 24px;

padding: 0 5px;

margin: 0;

}

.tag_list h5 a

{

color: #47381C;

}

.classify

{

margin: 10px 0;

padding: 4px 12px 8px;

}

.classify a

{

margin-right: 20px;

white-space: nowrap;

}

$(function(){

setTimeout(function(){

$.get("/wangxingwu_314/svc/GetTagContent",function(data){

$(".tag_list").html(data).show();

});

});

},500);

#popup_mask

{

position: absolute;

width: 100%;

height: 100%;

background: #000;

z-index: 9999;

left: 0px;

top: 0px;

opacity: 0.3;

filter: alpha(opacity=30);

display: none;

}

$(function(){

setTimeout(function(){

$(".comment_body:contains('回复')").each(function(index,item){

var u=$(this).text().split(':')[0].toString().replace("回复","")

var thisComment=$(this);

if(u)

{

$.getJSON("https://passport.csdn.net/get/nick?callback=?", {users: u}, function(a) {

if(a!=null&&a.data!=null&&a.data.length>0)

{

nick=a.data[0].n;

if(u!=nick)

{

thisComment.text(thisComment.text().replace(u,nick));

}

}

});

}

});

},200);

setTimeout(function(){

$(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })

},5000);

setTimeout(function(){

$(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })

},10000);

setTimeout(function(){

$(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })

},15000);

setTimeout(function(){

$("a img[src='http://js.tongji.linezing.com/stats.gif']").parent().css({"position":"absolute","left":"50%"});

},300);

});

function loginbox(){

var $logpop=$("#pop_win");

$logpop.html('');

$('#popup_mask').css({

opacity: 0.5,

width: $( document ).width() + 'px',

height: $( document ).height() + 'px'

});

$('#popup_mask').css("display","block");

$logpop.css( {

top: ($( window ).height() - $logpop.height())/ 2 + $( window

).scrollTop() + 'px',

left:($( window ).width() - $logpop.width())/ 2

} );

setTimeout( function () {

$logpop.show();

$logpop.css( {

opacity: 1

} );

}, 200 );

$('#popup_mask').unbind("click");

$('#popup_mask').bind("click", function(){

$('#popup_mask').hide();

var $clopop = $("#pop_win");

$("#common_ask_div_sc").css("display","none");

$clopop.css( {

opacity: 0

} );

setTimeout( function () {

$clopop.hide();

}, 350 );

return false;

});

}

个人资料

  • 访问:45606次
  • 积分:228
  • 等级:

    积分:228

  • 排名:千里之外
  • 原创:7篇
  • 转载:2篇
  • 译文:0篇
  • 评论:7条

$(function () {

$("#btnSubmit").click(function () {

search();

});

$("#frmSearch").submit(function () {

search();

return false;

});

function search()

{

var url = "http://so.csdn.net/so/search/s.do?q=" + encodeURIComponent($("#inputSearch").val()) + "&u=" + username + "&t=blog";

window.location.href = url;

}

});

文章分类
文章存档
最新评论

$(function () {

function __get_code_toolbar(snippet_id) {

return $("在CODE上查看代码片"

+ "派生到我的代码片");

}

$("[code_snippet_id]").each(function () {

__s_id = $(this).attr("code_snippet_id");

if (__s_id != null && __s_id != "" && __s_id != 0 && parseInt(__s_id) > 70020) {

__code_tool = __get_code_toolbar(__s_id);

$(this).prev().find(".tools").append(__code_tool);

}

});

$(".bar").show();

});

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值