欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android中怎么利用AlarmManager類實現(xiàn)鬧鐘功能-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關Android中怎么利用AlarmManager類實現(xiàn)鬧鐘功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

目前成都創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設計、鳳山網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

實現(xiàn)接收Alarm服務的AlarmReceiver類,該類比較簡單,在收到消息后用一個Toast來提示用戶,具體實現(xiàn)代碼如下:


public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {  Toast.makeText(context, "您設置的時間到了!",   Toast.LENGTH_SHORT).show(); }}

由于使用了BroadcastReceiver,因此我們需要在AndroidManifest.xml文件中對其進行聲明,如下:

<receiver android:name=".AlarmReceiver" android:process=":remote" />

接下來,在MainActivity中我們實現(xiàn)“設置鬧鐘”和“取消鬧鐘”的事件監(jiān)聽,讓我們來看一下具體實現(xiàn)代碼:

public class MainActivity extends Activity { private Button btnSet, btnCancel; private TextView info; private Calendar calendar; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  btnSet = (Button) findViewById(R.id.setalarm);  btnCancel = (Button) findViewById(R.id.cancelalarm);  info = (TextView) findViewById(R.id.info);  calendar = Calendar.getInstance();  btnSet.setOnClickListener(new OnClickListener() {   @Override   public void onClick(View v) {    // TODO Auto-generated method stub    calendar.setTimeInMillis(System.currentTimeMillis());    int mHour = calendar.get(Calendar.HOUR_OF_DAY);    int mMinute = calendar.get(Calendar.MINUTE);    new TimePickerDialog(MainActivity.this,      new TimePickerDialog.OnTimeSetListener() {       @Override       public void onTimeSet(TimePicker view,         int hourOfDay, int minute) {        // TODO Auto-generated method stub        calendar.setTimeInMillis(System.currentTimeMillis());        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);        calendar.set(Calendar.MINUTE, minute);        calendar.set(Calendar.SECOND, 0);        calendar.set(Calendar.MILLISECOND, 0);        // 建立Intent和PendingIntent來調用目標組件        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);        // 獲取鬧鐘管理的實例        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);        // 設置鬧鐘        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);        // 設置周期鬧鐘        am.setRepeating(AlarmManager.RTC_WAKEUP,          System.currentTimeMillis() + (10 * 1000),          (24 * 60 * 60 * 1000), pendingIntent);        String tmpS = "設置鬧鐘時間為" + format(hourOfDay)          + ":" + format(minute);        info.setText(tmpS);       }      }, mHour, mMinute, true).show();   }  });  btnCancel.setOnClickListener(new OnClickListener() {   @Override   public void onClick(View v) {    // TODO Auto-generated method stub    Intent intent = new Intent(MainActivity.this,      AlarmReceiver.class);    PendingIntent pendingIntent = PendingIntent.getBroadcast(      MainActivity.this, 0, intent, 0);    // 獲取鬧鐘管理實例    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);    // 取消    am.cancel(pendingIntent);    info.setText("鬧鐘已經(jīng)取消");   }  }); } // 格式化字符串7:3-->07:03 private String format(int x) {  String s = "" + x;  if (s.length() == 1) {   s = "0" + s;  }  return s; } @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.activity_main, menu);  return true; }}

在上述代碼中我們使用了PendingIntent,PendingIntent這個類用于處理即將發(fā)生的事情,PendingIntent可以看作是對Intent的包裝,通常通過getActivity、getBroadcast、getService來得到PendingIntent的實例,當前Activity并不能馬上啟動它所包含的Intent,而是在外部執(zhí)行PendingIntent時,調用Intent。正是由于PendingIntent中保存有當前App的context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執(zhí)行PendingIntent里的Intent,就算在執(zhí)行時當前App已經(jīng)不存在了,也能通過保存在PendingIntent里的Context照樣執(zhí)行Intent,另外還可以處理Intent執(zhí)行后的操作。常和AlarmManager和NotificationManager一起使用。

看完上述內容,你們對Android中怎么利用AlarmManager類實現(xiàn)鬧鐘功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站名稱:Android中怎么利用AlarmManager類實現(xiàn)鬧鐘功能-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://aaarwkj.com/article2/dddsoc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站電子商務、網(wǎng)站導航、網(wǎng)頁設計公司、品牌網(wǎng)站建設小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
毛片一区二区三区免费看| 久久av天堂在线观看| 国产精品欧美日韩高清| 夜夜春久久天堂亚洲精品| 高颜值美女后入内射视频| 日本熟女肥臀一区二区| 久久综合中文字幕一区| 国产精品中文字幕第一页| 日韩中文免费av一区| 国产av一区二区三区久久| 欧美高清一区二区三区不卡| 中文字幕国产成人在线视频| 久久综合婷婷亚洲五月| 国产成人三级视频网站| 偷窥偷拍原味一区二区三区| 国内丰满少妇嗷嗷叫在线播放| 欧美精品一区二区三区狠狠| 欧美日韩另类激情免费| 日本精品1区国产精品| 亚洲中文字幕乱码丝袜在线精品| 日韩人妻熟妇中文字幕| 日本在线高清不卡免费播放| 亚洲免费黄色大片网站| 亚洲最大色网在线观看| 亚洲国产欧美精品综合在线| 久久三级中文欧大战字幕| 亚洲av乱码国产精品观看| 97资源视频在线播放| 日韩精品一区二区三区av在线| 亚洲av网站女性向在线观看| 国产欧美日韩午夜激情| 日韩 欧美 国产 亚洲 综合| 亚洲国产日韩精品一区二| 国产福利午夜一区二区| 夜福利国产视频大屁股| 国产精品一区巨乳人妻| 日韩精品中文字幕影视| 亚洲国产精品第一区第二区| 国产传媒免费在线播放| 国产高清av免费观看| 国产精品久久一级黄片|