手机当中肯定是有闹钟的,这是不用说的,要不然就不是手机了。对吧。
下面我根据广播的方式写了一个闹钟,大家请看图:

下面让我们看一下原代码的是如何写的,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();                                                        }}

更多相关文章

  1. Cocos 2d-x 3.8 在ubuntu下使用QtCreator和Android(安卓)Studio
  2. Android(安卓)游戏开发中 OnTouchEvent() 触屏事件的性能优化
  3. android 格林尼治标准时间的 格式转换 (String To Date) 报错 ja
  4. android之View的启动过程
  5. 毫秒级别时间戳转换
  6. Android:Date、String、Long三种日期类型之间的相互转换
  7. *Android闹钟原理
  8. Android(安卓)连续点击两次退出
  9. Android关于Notification.Builder的使用

随机推荐

  1. 第十周智能手机开发学习笔记
  2. Android 对话框【Dialog】去除白色边框代
  3. android 用UDP做的心跳连接 小示例
  4. android 检测耳机是否插入
  5. TableLayout
  6. 手把手教你怎么去除Android(安卓)APP里面
  7. Android(安卓)大图压缩处理,避免OOM
  8. android全屏,去掉title栏的办法
  9. android单元测试Test run failed: Unable
  10. 简约之美Kotlin(一)Android(安卓)Studio创