日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發(fā)布時間:2025/3/21 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android:catation=quot;90quot;,Android中的AlarmManager的使用.htm 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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);

})();

wangxingwu_314的專欄

  • 目錄視圖

  • 摘要視圖

  • 訂閱

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 = "";

聚焦行業(yè)最佳實踐,BDTC 2016完整議程公布

微信小程序?qū)崙?zhàn)項目——點餐系統(tǒng)

程序員11月書訊,評論得書啦

Get IT技能知識庫,50個領(lǐng)域一鍵直達

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 += '' + obj.name + "??";

html += ' ';

html += ' ';

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

html += ' ';

});

if (html != "") {

setTimeout(function () {

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

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

}, 100);

}

}

} catch (err)

{ }

});

1、AlarmManager,顧名思義,就是“提醒”,是Android中常用的一種系統(tǒng)級別的提示服務(wù),在特定的時刻為我們廣播一個指定的Intent。簡單的說就是我們設(shè)定一個時間,然后在該時間到來時,AlarmManager為我們廣播一個我們設(shè)定的Intent,通常我們使用 PendingIntent,PendingIntent可以理解為Intent的封裝包,簡單的說就是在Intent上在加個指定的動作。在使用Intent的時候,我們還需要在執(zhí)行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個動作包含在內(nèi)了。

定義一個PendingIntent對象。

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

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

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

該方法用于設(shè)置一次性鬧鐘,第一個參數(shù)表示鬧鐘類型,第二個參數(shù)表示鬧鐘執(zhí)行時間,第三個參數(shù)表示鬧鐘響應(yīng)動作。

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

該方法用于設(shè)置重復鬧鐘,第一個參數(shù)表示鬧鐘類型,第二個參數(shù)表示鬧鐘首次執(zhí)行時間,第三個參數(shù)表示鬧鐘兩次執(zhí)行的間隔時間,第三個參數(shù)表示鬧鐘響應(yīng)動作。

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

該方法也用于設(shè)置重復鬧鐘,與第二個方法相似,不過其兩個鬧鐘執(zhí)行的間隔時間不是固定的而已。

3、三個方法各個參數(shù)詳悉:

(1)int type: 鬧鐘的類型,常用的有5個值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示鬧鐘在手機睡眠狀態(tài)下不可用,該狀態(tài)下鬧鐘使用相對時間(相對于系統(tǒng)啟動開始),狀態(tài)值為3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示鬧鐘在睡眠狀態(tài)下會喚醒系統(tǒng)并執(zhí)行提示功能,該狀態(tài)下鬧鐘也使用相對時間,狀態(tài)值為2;

AlarmManager.RTC表示鬧鐘在睡眠狀態(tài)下不可用,該狀態(tài)下鬧鐘使用絕對時間,即當前系統(tǒng)時間,狀態(tài)值為1;

AlarmManager.RTC_WAKEUP表示鬧鐘在睡眠狀態(tài)下會喚醒系統(tǒng)并執(zhí)行提示功能,該狀態(tài)下鬧鐘使用絕對時間,狀態(tài)值為0;

AlarmManager.POWER_OFF_WAKEUP表示鬧鐘在手機關(guān)機狀態(tài)下也能正常進行提示功能,所以是5個狀態(tài)中用的最多的狀態(tài)之一,該狀態(tài)下鬧鐘也是用絕對時間,狀態(tài)值為4;不過本狀態(tài)好像受SDK版本影響,某些版本并不支持;

(2)long startTime: 鬧鐘的第一次執(zhí)行時間,以毫秒為單位,可以自定義時間,不過一般使用當前時間。需要注意的是,本屬性與第一個屬性(type)密切相關(guān),如果第一個參數(shù)對 應(yīng)的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本屬性就得使用相對時間(相對于 系統(tǒng)啟動時間來說),比如當前時間就表示為:SystemClock.elapsedRealtime();如果第一個參數(shù)對應(yīng)的鬧鐘使用的是絕對時間 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本屬性就得使用絕對時間,比如當前時間就表示

為:System.currentTimeMillis()。

(3)long intervalTime:對于后兩個方法來說,存在本屬性,表示兩次鬧鐘執(zhí)行的間隔時間,也是以毫秒為單位。

(4)PendingIntent pi: 綁定了鬧鐘的執(zhí)行動作,比如發(fā)送一個廣播、給出提示等等。PendingIntent是Intent的封裝類。需要注意的是,如果是通過啟動服務(wù)來實現(xiàn)鬧鐘提 示的話,PendingIntent對象的獲取就應(yīng)該采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通過廣播來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應(yīng)該采用 PendingIntent.getBroadcast(Context

c,int i,Intent intent,int j)方法;如果是采用Activity的方式來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應(yīng)該采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。

4.舉例說明:定義一個鬧鐘,5秒鐘重復響應(yīng)。

(1)MainActivity,在onCreate中完成:

[java]

view plain

copyprint?
  • //創(chuàng)建Intent對象,action為ELITOR_CLOCK,附加信息為字符串“你該打醬油了”??
  • Intent?intent?=?new?Intent("ELITOR_CLOCK");??
  • intent.putExtra("msg","你該打醬油了");????
  • ??
  • //定義一個PendingIntent對象,PendingIntent.getBroadcast包含了sendBroadcast的動作。??
  • //也就是發(fā)送了action?為"ELITOR_CLOCK"的intent
  • PendingIntent?pi?=?PendingIntent.getBroadcast(this,0,intent,0);????
  • ??
  • //AlarmManager對象,注意這里并不是new一個對象,Alarmmanager為系統(tǒng)級服務(wù)??
  • AlarmManager?am?=?(AlarmManager)getSystemService(ALARM_SERVICE);????
  • ??
  • //設(shè)置鬧鐘從當前時間開始,每隔5s執(zhí)行一次PendingIntent對象pi,注意第一個參數(shù)與第二個參數(shù)的關(guān)系??
  • //?5秒后通過PendingIntent?pi對象發(fā)送廣播??
  • am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);??
  • //創(chuàng)建Intent對象,action為ELITOR_CLOCK,附加信息為字符串“你該打醬油了”

    Intent intent = new Intent("ELITOR_CLOCK");

    intent.putExtra("msg","你該打醬油了");

    //定義一個PendingIntent對象,PendingIntent.getBroadcast包含了sendBroadcast的動作。

    //也就是發(fā)送了action 為"ELITOR_CLOCK"的intent

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

    //AlarmManager對象,注意這里并不是new一個對象,Alarmmanager為系統(tǒng)級服務(wù)

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

    //設(shè)置鬧鐘從當前時間開始,每隔5s執(zhí)行一次PendingIntent對象pi,注意第一個參數(shù)與第二個參數(shù)的關(guān)系

    // 5秒后通過PendingIntent pi對象發(fā)送廣播

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

    那么啟動MainActivity之后,由于定義了AlarmManager am,并且調(diào)用了am.setRepeating(...)函數(shù),則系統(tǒng)每隔5s將會通過pi啟動intent發(fā)送廣播,其action為ELITOR_CLOCK。所以我們需要在Manifest.xml中注冊一個receiver,同時自己定義一個廣播接收器類。

    (2)定義一個廣播接收器類MyReceiver,重寫onReceive()函數(shù)。

    [java]

    view plain

    copyprint?
  • 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();??
  • ????}??
  • ??
  • }??
  • 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中注冊廣播接收器:

    [html]

    view plain

    copyprint?
  • <receiver?android:name=".MyReceiver">??
  • ????????<intent-filter>??
  • ????????????<action?android:name="ELITOR_CLOCK"?/>??
  • ????????</intent-filter>??
  • </receiver>??
  • <receiver android:name=".MyReceiver">

    <intent-filter>

    <action android:name="ELITOR_CLOCK" />

    </intent-filter>

    </receiver>

    補充:設(shè)置指定的時間啟動廣播。

    [html]

    view plain

    copyprint?

    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);//也可以填數(shù)字,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);

    //設(shè)定時間為 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);

    //設(shè)置一個PendingIntent對象,發(fā)送廣播

    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);

    //獲取AlarmManager對象

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

    //時間到時,執(zhí)行PendingIntent,只執(zhí)行一次

    //AlarmManager.RTC_WAKEUP休眠時會運行,如果是AlarmManager.RTC,在休眠時不會運行

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

    //如果需要重復執(zhí)行,使用上面一行的setRepeating方法,倒數(shù)第二參數(shù)為間隔時間,單位為毫秒

    }

    }

    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);

    });

    /*博客內(nèi)容頁下方Banner-728*90,創(chuàng)建于2014-7-3*/

    var cpro_id = "u1607657";

    查看評論

    * 以上用戶言論只代表其個人觀點,不代表CSDN網(wǎng)站的觀點或立場

    var fileName = '8060312';

    var commentscount = 7;

    var islock = false

    $(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;

    });

    }

    個人資料

    wangxingwu_314

    • 訪問:45606次
    • 積分:228
    • 等級:

      積分:228

    • 排名:千里之外
    • 原創(chuàng):7篇
    • 轉(zhuǎn)載: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;

    }

    });

    文章分類
    • Android應(yīng)用開發(fā)(7)

    文章存檔2016年05月(1)2013年04月(1)2012年11月(4)2012年10月(3)

    閱讀排行

    • Android中的AlarmManager的使用(40723)

    • Android Selector的實現(xiàn)原理(1267)

    • 類似于選擇城市相同的首字母放在一起的效果(599)

    • Android AppWidget開發(fā)心得(453)

    • Android自定義屬性,attr format取值類型(271)

    • GIT的使用(255)

    • Android中的部分筆記(249)

    • SimpleDateFormat的方法使用(224)

    • 實現(xiàn)QQ空間評論列表樣式(149)

    評論排行
    • Android中的AlarmManager的使用(7)

    • 類似于選擇城市相同的首字母放在一起的效果(0)

    • GIT的使用(0)

    • Android Selector的實現(xiàn)原理(0)

    • SimpleDateFormat的方法使用(0)

    • Android自定義屬性,attr format取值類型(0)

    • Android中的部分筆記(0)

    • Android AppWidget開發(fā)心得(0)

    • 實現(xiàn)QQ空間評論列表樣式(0)

    推薦文章
      • * RxJava詳解,由淺入深
      • target="_blank">* 倍升工作效率的小策略

      • target="_blank">* Android熱修復框架AndFix原理解析及使用

      • * “區(qū)塊鏈”究竟是什么鬼
      • target="_blank">* 架構(gòu)設(shè)計:系統(tǒng)存儲-MySQL主從方案業(yè)務(wù)連接透明化(中)

    最新評論
    • Android中的AlarmManager的使用

      u010032372:

      2016-8-1 學習

    • Android中的AlarmManager的使用

      iiChrome:

      @a1030260075:大哥,你能不能不要這么逗。不是PendingIntent.getActiv...

    • Android中的AlarmManager的使用

      a1030260075:

      你好,寫的很詳細,但是有一個地方有問題吧你都說了activity中應(yīng)該使用PendingIntent...

    • Android中的AlarmManager的使用

      moonlehehe:

      PendingIntent對象的獲取就采用Pending.getService(Context c,...

    • Android中的AlarmManager的使用

      FansUnion:

      寫得不錯啊

    • Android中的AlarmManager的使用

      TuRanLeiLiao:

      看到你該打醬油了我就笑了,,我是不是太無聊了

    • Android中的AlarmManager的使用

      u011731233:

      內(nèi)容不錯,不過排版很多內(nèi)容重復了

    $(function () {

    function __get_code_toolbar(snippet_id) {

    return $(""

    + "");

    }

    $("[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

    原始數(shù)據(jù)

    按行查看

    歷史

    《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的android:catation=quot;90quot;,Android中的AlarmManager的使用.htm的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。