Android鬧鐘設置

 鬧鐘在生活中最常見瞭,在Android中可以通過AlarmManager來實現鬧鐘,AlarmManager類專門用來設置在某個指定的時間去完成指定的時間。AlarmManager就會通過onReceive()方法去執行這些事件,就算系統處於待機狀態,同樣不會影響運行。可以通過Context.getSystemService方法來獲得該服務。AlarmManager中的方法不少,如下:
 

方法 說明
Cancel 取消AlarmManager服務
Set 設置AlarmManager服務
setInexactRepeating 設置不精確周期
SetRepeating 設置精確周期
setTimeZone 設置時區

 
        要實現鬧鐘,首先需要創建一個繼承自BroadcastReceiver的類,實現onReceive方法來接受這個Alarm服務,然後通過建立Intent和PendingIntent連接來調用Alarm組件。通過TimerPickerDialog來設置鬧鈴時間,當時間到瞭我們指定的時間後onReceiver方法接受到Alarm服務後的界面。
首先實現接受Alarm服務的AlarmReceiver類,用Toast類提示用戶
 

[java] public class AlarmReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
        // TODO Auto-generated method stub  
        Toast.makeText(arg0, "你設置的鬧鈴時間到瞭", Toast.LENGTH_LONG).show(); 
    } 
 

public class AlarmReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context arg0, Intent arg1) {
  // TODO Auto-generated method stub
  Toast.makeText(arg0, "你設置的鬧鈴時間到瞭", Toast.LENGTH_LONG).show();
 }

}
 

 

由於使用瞭BroadcastReceiver服務,因此需要再AndroidManifest.xml中進行聲明:

 

[java] <receiver  
            android:name=".AlarmReceiver" 
            android:process=":remote">             
</receiver> 
然後需要設置鬧鈴和取消鬧鈴的實踐進行監聽: 
package cn.edu.pku; 
 
import java.util.Calendar; 
 
import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.app.TimePickerDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.TimePicker; 
 
public class AlarmActivity extends Activity { 
    /** Called when the activity is first created. */ 
     
    Button  mButton1; 
    Button  mButton2; 
     
    TextView mTextView; 
     
    Calendar calendar; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        calendar=Calendar.getInstance(); 
         
        mTextView=(TextView)findViewById(R.id.TextView01); 
        mButton1=(Button)findViewById(R.id.Button01); 
        mButton2=(Button)findViewById(R.id.Button02); 
         
        mButton1.setOnClickListener(new View.OnClickListener() { 
             
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                calendar.setTimeInMillis(System.currentTimeMillis()); 
                int hour = calendar.get(Calendar.HOUR_OF_DAY); 
                int minute = calendar.get(Calendar.MINUTE); 
                new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() { 
                     
                    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 intent = new Intent(AlarmActivity.this, AlarmReceiver.class); 
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0); 
                        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
                        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
                        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000),  
                                (24 * 60 * 60 * 1000), pendingIntent); 
                        String tmps = "設置鬧鐘時間為" + format(hourOfDay) + ":" +format(minute); 
                        mTextView.setText(tmps); 
                    } 
                }, hour, minute, true).show(); 
            } 
        }); 
         
        mButton2.setOnClickListener(new View.OnClickListener() { 
             
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class); 
                PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0); 
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
                alarmManager.cancel(pendingIntent); 
                mTextView.setText("鬧鈴已取消!"); 
            } 
        }); 
    } 
     
    private String format(int time){ 
        String str = "" + time; 
        if(str.length() == 1){ 
            str = "0" + str; 
        } 
        return str; 
    } 

<receiver
            android:name=".AlarmReceiver"
            android:process=":remote">           
</receiver>
然後需要設置鬧鈴和取消鬧鈴的實踐進行監聽:
package cn.edu.pku;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */
 
 Button mButton1;
 Button mButton2;
 
 TextView mTextView;
 
 Calendar calendar;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        calendar=Calendar.getInstance();
  
  mTextView=(TextView)findViewById(R.id.TextView01);
  mButton1=(Button)findViewById(R.id.Button01);
  mButton2=(Button)findViewById(R.id.Button02);
  
  mButton1.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    calendar.setTimeInMillis(System.currentTimeMillis());
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
     
     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 intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000),
        (24 * 60 * 60 * 1000), pendingIntent);
      String tmps = "設置鬧鐘時間為" + format(hourOfDay) + ":" +format(minute);
      mTextView.setText(tmps);
     }
    }, hour, minute, true).show();
   }
  });
  
  mButton2.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    mTextView.setText("鬧鈴已取消!");
   }
  });
    }
   
    private String format(int time){
     String str = "" + time;
     if(str.length() == 1){
      str = "0" + str;
     }
     return str;
    }
}

 

 

效果如下:

 

設置鬧鈴

 

 

 

現在時間到設置鬧鈴的時間:

 

 

 

取消鬧鈴:

 

摘自 北京大學-Google Android實驗室

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *