欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android基于廣播事件機(jī)制實現(xiàn)簡單定時提醒功能代碼

 更新時間:2015年10月17日 16:51:27   作者:GDSongrenjun  
這篇文章主要介紹了Android基于廣播事件機(jī)制實現(xiàn)簡單定時提醒功能代碼,較為詳細(xì)的分析了Android廣播事件機(jī)制及提醒功能的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

1.Android廣播事件機(jī)制

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

2.實現(xiàn)簡單的定時提醒功能

主要包括三部分部分:

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

現(xiàn)在我們來具體實現(xiàn)這三部分:

2.1 如何定時,從而發(fā)出廣播呢?

現(xiàn)在的手機(jī)都有鬧鐘的功能,我們可以利用系統(tǒng)提供的鬧鐘功能,來定時,即發(fā)出廣播。具體地,在Android開發(fā)中可以用AlarmManager來實現(xiàn)。

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

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

詳細(xì)代碼如下(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,并實現(xiàn)onReceive()方法。當(dāng)BroadcastReceiver接收到廣播后,就會去執(zhí)行OnReceive()方法。所以,我們在OnReceive()方法中加上代碼,當(dāng)接收到廣播后就跳到顯示提醒信息的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);
  }
}

注意:創(chuàng)建完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來顯示提示內(nèi)容和并一個button來取消提醒。

其中,創(chuàng)建Notification主要包括:

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

詳細(xì)代碼如下: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程序設(shè)計有所幫助。

相關(guān)文章

最新評論