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

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

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

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

1.Android廣播事件機制

Android的廣播事件處理類似于普通的事件處理。不同之處在于,后者是靠點擊按鈕這樣的組件行為來觸發(fā),而前者是通過構(gòu)建Intent對象,使用sentBroadcast()方法來發(fā)起一個系統(tǒng)級別的事件廣播來傳遞信息。廣播事件的接收是通過定義一個繼承Broadcast Receiver的類實現(xiàn)的,繼承該類后覆蓋其onReceive()方法,在該方法中響應事件。Android系統(tǒng)中定義了很多標準的Broadcast Action來響應系統(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)在的手機都有鬧鐘的功能,我們可以利用系統(tǒng)提供的鬧鐘功能,來定時,即發(fā)出廣播。具體地,在Android開發(fā)中可以用AlarmManager來實現(xiàn)。

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

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

詳細代碼如下(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()方法。當BroadcastReceiver接收到廣播后,就會去執(zhí)行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);
  }
}

注意:創(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)級得服務NotificationManager,通過 Context.getSystemService(NOTIFICATION_SERVICE)獲得。
2)實例化Notification對象,并設(shè)置各種我們需要的屬性,比如:設(shè)置聲音。
3)調(diào)用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程序設(shè)計有所幫助。

相關(guān)文章

  • 使用Glide實現(xiàn)高斯模糊效果

    使用Glide實現(xiàn)高斯模糊效果

    這篇文章主要為大家詳細介紹了使用Glide實現(xiàn)高斯模糊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Android實現(xiàn)應用內(nèi)置語言切換功能

    Android實現(xiàn)應用內(nèi)置語言切換功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)應用內(nèi)置語言切換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android改變手機屏幕朝向的方法

    Android改變手機屏幕朝向的方法

    這篇文章主要介紹了Android改變手機屏幕朝向的方法,涉及Android動態(tài)更改手機屏幕朝向的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Android實現(xiàn)音頻錄音與播放

    Android實現(xiàn)音頻錄音與播放

    這篇文章主要為大家詳細介紹了Android實現(xiàn)音頻錄音與播放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • flutter Bloc 更新后事件同步與異步詳解

    flutter Bloc 更新后事件同步與異步詳解

    這篇文章主要為大家介紹了flutter Bloc 更新后事件同步與異步詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android編程視頻播放API之MediaPlayer用法示例

    Android編程視頻播放API之MediaPlayer用法示例

    這篇文章主要介紹了Android編程視頻播放API之MediaPlayer用法,結(jié)合實例形式分析了基于Android API實現(xiàn)視頻播放功能的多媒體文件讀取、判斷、事件響應及流媒體播放等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • Android ContentProvider實現(xiàn)獲取手機聯(lián)系人功能

    Android ContentProvider實現(xiàn)獲取手機聯(lián)系人功能

    這篇文章主要為大家詳細介紹了Android ContentProvider實現(xiàn)獲取手機聯(lián)系人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android View實現(xiàn)圓形進度條

    Android View實現(xiàn)圓形進度條

    這篇文章主要為大家詳細介紹了Android View實現(xiàn)圓形進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android自定義View圓形和拖動圓跟隨手指拖動

    Android自定義View圓形和拖動圓跟隨手指拖動

    這篇文章主要介紹了Android自定義View圓形和拖動圓跟隨手指拖動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android模擬開關(guān)按鈕點擊打開動畫(屬性動畫之平移動畫)

    Android模擬開關(guān)按鈕點擊打開動畫(屬性動畫之平移動畫)

    這篇文章主要介紹了Android模擬開關(guān)按鈕點擊打開動畫(屬性動畫之平移動畫)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09

最新評論