Android 实现闹钟功能
一、手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以在程序中设置某个应用程序在未来的某个时刻被执行。当AlarmManager定时时间到了之后,当初注册的Intent对象将会被系统广播,进而启动目标程序。
二、在程序运行时当需要使用AlarmManager类时,可以通过Context对象的getSystemService(Context.ALARM_SERVICE)方法来获得AlarmManager对象。
以下程序中使用时间选择对话框设置闹钟的时间。
main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btn" android:text="设置闹钟" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AlarmActivity" /> <!-- android:process=":remote": 新开一个进程 --> <receiver android:name=".AlarmReceiver" android:process=":remote"/> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
AlarmReceiver类
package com.ljq.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Intent i=new Intent(context, AlarmActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
AlarmActivity类
package com.ljq.activity; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; public class AlarmActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //显示对话框 new AlertDialog.Builder(AlarmActivity.this). setTitle("闹钟").//设置标题 setMessage("时间到了!").//设置内容 setPositiveButton("知道了", new OnClickListener(){//设置按钮 public void onClick(DialogInterface dialog, int which) { AlarmActivity.this.finish();//关闭Activity } }).create().show(); } }
MainActivity类
import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.Dialog; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity{ private Button btn=null; private AlarmManager alarmManager=null; Calendar cal=Calendar.getInstance(); final int DIALOG_TIME = 0; //设置对话框id @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View view) { showDialog(DIALOG_TIME);//显示时间选择对话框 } }); } @Override protected Dialog onCreateDialog(int id) { Dialog dialog=null; switch (id) { case DIALOG_TIME: dialog=new TimePickerDialog( this, new TimePickerDialog.OnTimeSetListener(){ public void onTimeSet(TimePicker timePicker, int hourOfDay,int minute) { Calendar c=Calendar.getInstance();//获取日期对象 c.setTimeInMillis(System.currentTimeMillis()); //设置Calendar对象 c.set(Calendar.HOUR, hourOfDay); //设置闹钟小时数 c.set(Calendar.MINUTE, minute); //设置闹钟的分钟数 c.set(Calendar.SECOND, 0); //设置闹钟的秒数 c.set(Calendar.MILLISECOND, 0); //设置闹钟的毫秒数 Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); //创建Intent对象 PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); //创建PendingIntent //alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); //设置闹钟 alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi); //设置闹钟,当前时间就唤醒 Toast.makeText(MainActivity.this, "闹钟设置成功", Toast.LENGTH_LONG).show();//提示用户 } }, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false); break; } return dialog; } }
运行结果