软件工程日报十八——实现闹钟定时功能

今天学习如何在安卓上实现闹钟定时功能

xml 文件源码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AlarmActivity">

    <TimePicker
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="设置闹钟" />

</RelativeLayout>

闹钟计时文件源码如下:

package com.example.test06;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
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;

import androidx.annotation.Nullable;

import java.util.Calendar;

public class ClockActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clock);
        final TimePicker timePicker =(TimePicker) findViewById(R.id.time);
        timePicker.setIs24HourView(true);
        Button button1=(Button) findViewById(R.id.set);
        //为设置闹钟按钮添加事件监听器
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置闹钟
                Intent intent=new Intent(ClockActivity.this,AlarmActivity.class);//创建一个Intent对象
                PendingIntent pendingIntent=PendingIntent.getActivity(ClockActivity.this,0,intent,PendingIntent.FLAG_MUTABLE);//获取显示闹钟的对象
                AlarmManager alarm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);//获取alarmManager对象
                Calendar c=Calendar.getInstance();//获取日历对象
                c.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());//设置闹钟小时数
                c.set(Calendar.MINUTE,timePicker.getCurrentMinute());//设置闹钟分钟数
                c.set(Calendar.SECOND,0);//设置闹钟秒数
                alarm.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);//设置一个闹钟
                Toast.makeText(ClockActivity.this,"闹钟设置成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

到时提示框文件源码如下

package com.example.test06;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class AlarmActivity  extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog alert=new AlertDialog.Builder(this).create();
         //alert.setIcon(R.drawable.alarm);
        alert.setTitle("时间到了");
        alert.setMessage("需要干活了");
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setClass(AlarmActivity.this,add.class);
                startActivity(intent);
                Toast.makeText(AlarmActivity.this,"干活",Toast.LENGTH_SHORT).show();
            }
        });
        alert.show();
    }
}

运行结果如下:

 

 

提示框

 

posted @ 2023-03-16 17:46  伽澄  阅读(58)  评论(0编辑  收藏  举报