欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

定时闹钟

程序员文章站 2022-04-02 10:54:04
...

1、功能介绍

点击按钮 设置闹钟 –》时间到–》闹铃提醒
定时闹钟

定时闹钟

2、代码结构

定时闹钟

3、主要功能代码

MainActivity.java

package com.example.menglux.myalarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TimePicker;

import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


    private String  TAG = "MainActivity: ";
    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    private static  int    num = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //闹钟管理器
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    }
    public void setAlarmOne(View view){


        //获取当前系统的时间
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);//时
        int minute=calendar.get(Calendar.MINUTE);//分
        //先要弹出时间对话框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                //确定选择好的时间  给了Calendar
                Calendar c=Calendar.getInstance();
/*                c.set(Calendar.YEAR,2018);
               // c.set(Calendar.MONTH,Calendar.JUNE);//也可以填数字,0-11,一月为0
                c.set(Calendar.MONTH,7);//也可以填数字,0-11,一月为0
                c.set(Calendar.DAY_OF_MONTH, 1);*/
                c.set(Calendar.HOUR_OF_DAY,i);
                c.set(Calendar.MINUTE,i1);
                c.set(Calendar.SECOND,20);

                System.out.println(TAG + "设定的闹钟时间:" + i + " 时 " +  i1  + " 分");
                Intent intent=new Intent();
                intent.setAction("com.example.alarmandnotice_android.DCAT");

                //对于Android 8.0 系统 会出现收不到 闹铃广播的问题  添加下面内容  就是接受广播的类所在位置
                intent.setComponent(new ComponentName("com.example.menglux.myalarm","com.example.menglux.myalarm.BroadcatReceive"));


                //设置多个闹钟 只是需要第二个 参数 不同
                //  PendingIntent pendingIntent= PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
                pendingIntent = PendingIntent.getBroadcast(MainActivity.this,num,intent,0);

                System.out.println(TAG + "第几个闹钟:" + num);

                //android Api的改变不同版本中设 置有所不同
                if(Build.VERSION.SDK_INT<19){
                    //设置闹钟
                    alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
                }else{
                    alarmManager.setExact(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
                }

                num ++ ;
            }
        },hour,minute,false);
        timePickerDialog.show();
    }


    //删除闹钟
    public void cancelAlarmCycle(View view){
        alarmManager.cancel(pendingIntent);
    }

}

BroadcatReceive.java

package com.example.menglux.myalarm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by lum on 2018/7/31.
 */

public class BroadcatReceive extends BroadcastReceiver {
    private  String  TAG = "BroadcatReceive: ";
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(TAG + "获得广播");
        if ("com.example.alarmandnotice_android.DCAT".equals(intent.getAction())){
            System.out.println(TAG + "获得闹铃广播");

            //接受到闹铃广播 跳转界面 ,当有多个闹铃时 ,可通过获取系统时间 与 保存设定个的闹铃时间进行对比 来区分不同闹铃

            Intent intentdata = new Intent(context,AlarmShowActivity.class);
            intentdata.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            System.out.println(TAG + "lum_2");
            context.startActivity(intentdata);
            System.out.println(TAG + "lum_3");
        }
    }
}

AlarmShowActivity.java

package com.example.menglux.myalarm;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by lum on 2018/7/31.
 */

public class AlarmShowActivity extends AppCompatActivity {

    private String TAG = "AlarmShowActivity: " ;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        System.out.println(TAG + "");
        getCurrentTime();
        mediaPlayer = MediaPlayer.create(this, R.raw.nlch);
        mediaPlayer.start();
    }



       //得到当前时间
      private void getCurrentTime() {
        //24小时制
        SimpleDateFormat dFormat24Huur = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
          //SimpleDateFormat dFormat24Huur = new SimpleDateFormat("yyyy-M-d H:m", Locale.CHINA);
          String date24 = dFormat24Huur.format(new java.util.Date());
        //12小时制
        SimpleDateFormat dFormat12Hour = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
          String date12 = dFormat12Hour.format(new java.util.Date());
        System.out.println(TAG + "获得当前的系统时间:" + "24小时: " +  date24  + "  12小时: " + date12);
    }

    public void stop(View view){
        mediaPlayer.stop();
        finish();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.menglux.myalarm">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name=".AlarmShowActivity"/>


        <receiver android:name=".BroadcatReceive">
            <intent-filter>
                <action android:name="com.example.alarmandnotice_android.DCAT"></action>
            </intent-filter>
        </receiver>

    </application>

</manifest>

demo 下载

文献参考:
Android之实现简单小闹钟
https://blog.csdn.net/d_shadow/article/details/55144742

Android实现简单的闹钟
https://blog.csdn.net/dongye2016/article/details/78052214

Android广播事件机制及应用(实现简单的定时提醒功能)
https://blog.csdn.net/wanglang3081/article/details/7456684

安卓小项目实战之–定时提醒备忘录
https://github.com/zeschi/Android-AlarmManagerClock-master

相关标签: clock