• <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開發
            久久久受www免费人成| 久久成人国产精品| 国产aⅴ激情无码久久| 久久国产乱子伦免费精品| 久久综合狠狠综合久久激情 | 婷婷综合久久狠狠色99h| 日本五月天婷久久网站| 久久久久久久国产免费看| 久久这里只有精品18| 国产精品gz久久久| 久久国产精品99国产精| 久久国产综合精品五月天| 久久超碰97人人做人人爱| 欧洲性大片xxxxx久久久| 国产精品久久自在自线观看| 国内精品久久国产| 亚洲国产精品久久| 午夜精品久久久久久99热| 色播久久人人爽人人爽人人片aV| 日韩av无码久久精品免费| 性做久久久久久免费观看| 亚洲乱亚洲乱淫久久| 国产精品国色综合久久| 77777亚洲午夜久久多人| 亚洲精品国精品久久99热| 青青青青久久精品国产| 久久无码中文字幕东京热| 青青热久久国产久精品| 久久中文字幕视频、最近更新| 91久久精品无码一区二区毛片| 7777久久亚洲中文字幕| 久久久久亚洲Av无码专| 久久精品国产亚洲AV高清热| 久久ZYZ资源站无码中文动漫| 99精品国产综合久久久久五月天| 伊人久久一区二区三区无码| 欧美粉嫩小泬久久久久久久| 中文字幕久久亚洲一区| 国产亚洲美女精品久久久2020| 伊人久久大香线蕉av一区| 国内精品综合久久久40p|