国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android基于廣播事件機制實現簡單定時提醒功能代碼

2020-04-11 11:22:51
字體:
來源:轉載
供稿:網友

本文實例講述了Android基于廣播事件機制實現簡單定時提醒功能代碼。分享給大家供大家參考,具體如下:

1.Android廣播事件機制

Android的廣播事件處理類似于普通的事件處理。不同之處在于,后者是靠點擊按鈕這樣的組件行為來觸發,而前者是通過構建Intent對象,使用sentBroadcast()方法來發起一個系統級別的事件廣播來傳遞信息。廣播事件的接收是通過定義一個繼承Broadcast Receiver的類實現的,繼承該類后覆蓋其onReceive()方法,在該方法中響應事件。Android系統中定義了很多標準的Broadcast Action來響應系統廣播事件。例如:ACTION_TIME_CHANGED(時間改變時觸發)。但是,我們也可以自己定義Broadcast Receiver接收廣播事件。

2.實現簡單的定時提醒功能

主要包括三部分部分:

1) 定時 - 通過定義Activity發出廣播
2) 接收廣播 - 通過實現BroadcastReceiver接收廣播
3)  提醒 - 并通過Notification提醒用戶

現在我們來具體實現這三部分:

2.1 如何定時,從而發出廣播呢?

現在的手機都有鬧鐘的功能,我們可以利用系統提供的鬧鐘功能,來定時,即發出廣播。具體地,在Android開發中可以用AlarmManager來實現。

AlarmManager 提供了一種系統級的提示服務,允許你安排在某個時間執行某一個服務。
AlarmManager的使用步驟說明如下:

1)獲得AlarmManager實例: AlarmManager對象一般不直接實例化,而是通過Context.getSystemService(Context.ALARM_SERVIECE) 方法獲得
2)定義一個PendingIntent來發出廣播。
3)調用AlarmManager的相關方法,設置定時、重復提醒等功能。

詳細代碼如下(ReminderSetting.java):

package com.Reminder;import java.util.Calendar;import android.app.Activity;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;/*** trigger the Broadcast event and set the alarm*/public class ReminderSetting extends Activity {  Button btnEnable;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    /* create a button. When you click the button, the alarm clock is enabled */    btnEnable=(Button)findViewById(R.id.btnEnable);    btnEnable.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        setReminder(true);      }    });  }  /**   * Set the alarm    *    * @param b whether enable the Alarm clock or not    */  private void setReminder(boolean b) {    // get the AlarmManager instance     AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);    // create a PendingIntent that will perform a broadcast    PendingIntent pi= PendingIntent.getBroadcast(ReminderSetting.this, 0, new Intent(this,MyReceiver.class), 0);    if(b){      // just use current time as the Alarm time.       Calendar c=Calendar.getInstance();      // schedule an alarm      am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);    }    else{      // cancel current alarm      am.cancel(pi);    }  }}

2.2 接收廣播

新建一個class 繼承BroadcastReceiver,并實現onReceive()方法。當BroadcastReceiver接收到廣播后,就會去執行OnReceive()方法。所以,我們在OnReceive()方法中加上代碼,當接收到廣播后就跳到顯示提醒信息的Activity。具體代碼如下( MyReceiver.java):

package com.Reminder;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;/*** Receive the broadcast and start the activity that will show the alarm*/public class MyReceiver extends BroadcastReceiver {  /**   * called when the BroadcastReceiver is receiving an Intent broadcast.   */  @Override  public void onReceive(Context context, Intent intent) {    /* start another activity - MyAlarm to display the alarm */    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    intent.setClass(context, MyAlarm.class);    context.startActivity(intent);  }}

注意:創建完BroadcastReceiver后,需要在AndroidManifest.xml中注冊:

<receiver android:name=".MyReceiver">      <intent-filter>     <action android:name= "com.Reminder.MyReceiver" />   </intent-filter> </receiver>

2.3 提醒功能

新建一個Activity,我們在這個Activity中通過Android的Notification對象來提醒用戶。我們將添加提示音,一個TextView來顯示提示內容和并一個button來取消提醒。

其中,創建Notification主要包括:

1)獲得系統級得服務NotificationManager,通過 Context.getSystemService(NOTIFICATION_SERVICE)獲得。
2)實例化Notification對象,并設置各種我們需要的屬性,比如:設置聲音。
3)調用NotificationManager的notify()方法顯示Notification

詳細代碼如下:MyAlarm.java

package com.Reminder;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore.Audio;import android.view.View;import android.widget.Button;import android.widget.TextView;/*** Display the alarm information */public class MyAlarm extends Activity {  /**   * An identifier for this notification unique within your application   */  public static final int NOTIFICATION_ID=1;   @Override  protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.my_alarm);    // create the instance of NotificationManager    final NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);    // create the instance of Notification    Notification n=new Notification();    /* set the sound of the alarm. There are two way of setting the sound */     // n.sound=Uri.parse("file:///sdcard/alarm.mp3");    n.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "20");    // Post a notification to be shown in the status bar    nm.notify(NOTIFICATION_ID, n);    /* display some information */    TextView tv=(TextView)findViewById(R.id.tvNotification);    tv.setText("Hello, it's time to bla bla...");    /* the button by which you can cancel the alarm */    Button btnCancel=(Button)findViewById(R.id.btnCancel);    btnCancel.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View arg0) {        nm.cancel(NOTIFICATION_ID);        finish();      }    });  }}

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜梁县| 邻水| 本溪| 会东县| 北宁市| 西华县| 通城县| 浦北县| 苗栗市| 怀安县| 广昌县| 山阳县| 浦县| 虎林市| 且末县| 平和县| 公主岭市| 福鼎市| 沧源| 普格县| 墨竹工卡县| 巴南区| 玉屏| 西乌| 清河县| 江永县| 古浪县| 平泉县| 南宫市| 溧阳市| 柘荣县| 天祝| 息烽县| 丰顺县| 铁力市| 广宗县| 宁武县| 顺昌县| 顺昌县| 桦南县| 麦盖提县|