• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            life02

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              197 隨筆 :: 3 文章 :: 37 評論 :: 0 Trackbacks
            http://www.cnblogs.com/jico/archive/2010/11/03/1868361.html

            AlarmManager的使用機制有的稱呼為全局定時器,有的稱呼為鬧鐘。通過對它的使用,個人覺得叫全局定時器比較合適,其實它的作用和Timer有點相似。都有兩種相似的用法:(
            1)在指定時長后執行某項操作(2)周期性的執行某項操作

            AlarmManager對象配合Intent使用,可以定時的開啟一個Activity,發送一個BroadCast,或者開啟一個Service.

            下面的代碼詳細的介紹了兩種定時方式的使用:

             (
            1)在指定時長后執行某項操作

            代碼 

             

                   
            //操作:發送一個廣播,廣播接收后Toast提示定時操作完成
            Intent intent =new Intent(Main.this, alarmreceiver.class);
            intent.setAction(
            "short");
            PendingIntent sender
            =
            PendingIntent.getBroadcast(Main.
            this0, 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 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.
            this0, intent, 0);
            //開始時間
            long firstime=SystemClock.elapsedRealtime();

            AlarmManager am
            =(AlarmManager)getSystemService(ALARM_SERVICE);
              
            //5秒一個周期,不停的發送廣播
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
            , firstime, 
            5*1000, sender);

            復制代碼

             AlarmManager的setRepeating()相當于Timer的Schedule(task,delay,peroid);有點差異的地方時Timer這個方法是指定延遲多長時間

            以后開始周期性的執行task;

            AlarmManager的取消:(其中需要注意的是取消的Intent必須與啟動Intent保持絕對一致才能支持取消AlarmManager)
            代碼

            Intent intent 
            =new Intent(Main.this, alarmreceiver.class);
            intent.setAction(
            "repeating");
            PendingIntent sender
            =PendingIntent
            .getBroadcast(Main.
            this0, intent, 0);
            AlarmManager alarm
            =(AlarmManager)getSystemService(ALARM_SERVICE);
            alarm.cancel(sender);

            復制代碼

             
            posted on 2012-03-28 19:59 life02 閱讀(421) 評論(0)  編輯 收藏 引用 所屬分類: Android開發
            久久久精品国产sm调教网站| 久久久一本精品99久久精品88| 久久久精品波多野结衣| 亚洲中文字幕伊人久久无码| 麻豆AV一区二区三区久久| 久久亚洲精品中文字幕三区| 久久久艹| 国产99久久久国产精品~~牛 | 久久久久久久精品成人热色戒| 国产精品美女久久久| 久久亚洲AV无码精品色午夜| 久久亚洲精品视频| 久久精品欧美日韩精品| 一极黄色视频久久网站| 久久久久久综合一区中文字幕 | 亚洲αv久久久噜噜噜噜噜| 久久福利片| 久久国产免费直播| 久久精品人成免费| 亚洲中文字幕久久精品无码APP | 伊人久久综在合线亚洲2019| 久久亚洲AV无码精品色午夜麻豆 | 国产亚州精品女人久久久久久| 久久综合九色综合网站| 久久久亚洲AV波多野结衣| 欧美精品丝袜久久久中文字幕 | 久久人妻少妇嫩草AV蜜桃| 欧美亚洲另类久久综合| 久久国产精品99精品国产| 无码人妻久久一区二区三区| 久久青青色综合| 亚洲国产精品无码久久98| 国产69精品久久久久9999APGF | 国产精品久久久久一区二区三区| 久久精品国产秦先生| 99久久www免费人成精品| 国产精品免费久久| 精品久久久久久无码国产| 久久精品亚洲欧美日韩久久| 久久成人18免费网站| 久久亚洲熟女cc98cm|