当前位置:操作系统 > 安卓/Android >>

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); 
         &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,