手机当中肯定是有闹钟的,这是不用说的,要不然就不是手机了。对吧。
下面我根据广播的方式写了一个闹钟,大家请看图:
下面我根据广播的方式写了一个闹钟,大家请看图:
![1.jpg 1.jpg](http://www.eoeandroid.com/data/attachment/forum/201103/15/154804zzyemn1ow4nuyn3l.jpg)
下面让我们看一下原代码的是如何写的,
package com.smart;
import java.util.Calendar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
public class AlarmReceiver extends BroadcastReceiver {
/**
* 通过广播进行扫描,是否到达时间后再响起闹铃
*
* */
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences=context.getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);
String hour=String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
String minute=String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
String time=sharedPreferences.getString(hour+":"+minute, null);//小时与分,
if(time!=null){//判断是否为空,然后通过创建,
MediaPlayer mediaPlayer=MediaPlayer.create(context, R.raw.llb);
mediaPlayer.start();//开始
}
}
}
package com.smart;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class Main extends Activity implements OnClickListener{
private TextView alarmRecord;
private SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addAlarm=(Button)findViewById(R.id.addAlarm);
alarmRecord=(TextView)findViewById(R.id.alarmRecord);
addAlarm.setOnClickListener(this);
sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);
AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, intent, 0);
aManager.setRepeating(AlarmManager.RTC, 0, 60*1000, pendingIntent);
}
@Override
public void onClick(View v) {
View view=getLayoutInflater().inflate(R.layout.llb, null);//
final TimePicker timePicker=(TimePicker)view.findViewById(R.id.timepicker);
timePicker.setIs24HourView(true);//
new AlertDialog.Builder(this).setTitle("设置闹铃时间").setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//设置标题
@Override
public void onClick(DialogInterface dialog, int which) {
//按钮事件触发方法
String timeStr=String.valueOf(timePicker.getCurrentHour())+":"+String.valueOf(timePicker.getCurrentMinute());
alarmRecord.setText(alarmRecord.getText().toString()+"\n"+timeStr);
sharedPreferences.edit().putString(timeStr, timeStr).commit();
}
}).setNegativeButton("取消", null).show();
}
}