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

Android 闹钟

/*
 * Android 闹钟
 * Android API中提供了AlarmManager类,可以设置在指定的
 * 时间运行某些动作。
 * 主程序中以getSystemService(ALARM_SERVICE)取得AlarmManager
 * 并利用set()和setRepeating()两个方法来实现两种不同类型的闹钟。
 *
 * 1.只响一次的闹钟:当点击“设置闹钟”按钮时,会触发Button的
 * onClick事件,跳出TimePickerDialog来设置时间,设置完后,就以
 * PendingIntent.getBroadcast()生成PendingIntent,再利用
 * AlarmManager的set()将设置的时间与PendingIntent传入AlarmManager,
 * 最后以Toast提示用户以完成设置,并改变屏幕上的设置时间。
 *
 * 2.重复响起的闹钟:当点击“设置闹钟”按钮时,会触发Button的
 * onClick事件,跳出自定义的Layout的AlertDialog来设置开始时间与
 * 重复间隔。设置完成后,以PendingIntent.getBroadcast()生成
 * PendingIntent,再利用 AlarmManager的setReapting(),来设置开始
 * 时间、重复间隔与与PendingIntent传入AlarmManager,最后以Toast
 * 提示用户以完成设置,并改变屏幕上的设置时间。
 *
 * 程序中以cancel()这个方法来删除AlarmManager中设置的闹钟。
 */
import 略;
[java]
public class Ex06_10Activity extends Activity { 
    private TextView setTime1, setTime2; 
    private Button mButton1, mButton2, mButton3, mButton4; 
    private Calendar c = Calendar.getInstance(); 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 只响一次闹钟设置 
        setTime1 = (TextView) findViewById(R.id.setTime1); 
        setTime2 = (TextView) findViewById(R.id.setTime2); 
        mButton1 = (Button) findViewById(R.id.mButton1); 
        mButton2 = (Button) findViewById(R.id.mButton2); 
        mButton3 = (Button) findViewById(R.id.mButton3); 
        mButton4 = (Button) findViewById(R.id.mButton4); 
 
        mButton1.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                // 取得单击按钮时的时间作为TimePickerDialog的默认值 
                c.setTimeInMillis(System.currentTimeMillis()); 
                int mHour = c.get(Calendar.HOUR_OF_DAY); 
                int mMinute = c.get(Calendar.MINUTE); 
 
                // 跳出TimePickerDialog来设置时间 
                new TimePickerDialog(Ex06_10Activity.this, 
                        new TimePickerDialog.OnTimeSetListener() { 
 
                            @Override 
                            public void onTimeSet(TimePicker view, 
                                    int hourOfDay, int minute) { 
                                // TODO Auto-generated method stub 
                                // 取得设置后的时间,秒跟毫秒设为0 
                                c.setTimeInMillis(System.currentTimeMillis()); 
                                c.set(Calendar.HOUR_OF_DAY, hourOfDay); 
                                c.set(Calendar.MINUTE, minute); 
                                c.set(Calendar.SECOND, 0); 
                                c.set(Calendar.MILLISECOND, 0); 
                                // 指定闹钟设置的时间到时,要运行的CallAlarm.class 
                                Intent intent = new Intent( 
                                        Ex06_10Activity.this, CallAlarm.class); 
                                PendingIntent sender = PendingIntent 
                                        .getBroadcast( 
                                      &n

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