简单闹钟程序
1.个人习惯首先上布局main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟" />
<Button
android:id="@+id/canclebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消闹钟" />
</LinearLayout>
2.接着上活动AlarmClockActivity.java
package com.wang;
import java.text.Format;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmClockActivity extends Activity {
// //取得系统的时间的默认值
private Calendar calendar =Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button okbutton=(Button)findViewById(R.id.okbutton);
Button canclebutton=(Button)findViewById(R.id.canclebutton);
okbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//取得系统的时间的默认值
calendar.setTimeInMillis(System.currentTimeMillis());
int mHour=calendar.get(Calendar.HOUR_OF_DAY);
int mMinute=calendar.get(Calendar.MINUTE);
new TimePickerDialog(AlarmClockActivity.this,new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//取得设置的时间
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);
System.out.println("------------0-------");
//设置到指定的时间提醒
Intent intent =new Intent(AlarmClockActivity.this,TimeReciever.class);
System.out.println("------------q1-------");
//创建PendingIntent
PendingIntent sender=PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);
// 设置PendingIntent自运行一次
AlarmManager alarmManager;
alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//更新显示设置的闹钟时间
String tmps=format(hourOfDay)+":"+format(minute);
// toast 提示一下
Toast.makeText(AlarmClockActivity.this, "亲、设置的闹钟时间为:"+tmps,Toast.LENGTH_LONG).show();
}
private String format(int x) {
String s=""+x;
if (s.length()==1) {
s="0"+s;
}
return s;
}
}, mHour, mMinute, true).show();
}
});
//取消闹钟
canclebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(AlarmClockActivity.this,TimePicker.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);
//
AlarmManager alarmManager;
alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(AlarmClockActivity.this, "亲、闹钟已取消...", 3).show();
}
});
}
}
3.活动里需要一个类:TimeReciever.java
package com.wang;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TimeReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("---------2----------");
Toast.makeText(context, "亲、提示时间到了...", 3).show();
System.out.println("---------1----------");
}
}
4.亲、由于TimeReciever类继承了BroadcastReceiver,别忘了声明服务哦!
5.运行结果如下: