package com.example.daka; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; 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 java.util.Calendar; public class PointerActivity extends Activity { private Button btn; private AlarmManager alarmManager; //闹钟管理器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pointer); //获取闹钟管理器 alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); btn = (Button)findViewById(R.id.set_clock); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { setClock(view); } }); } public void setClock(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 view, int hourOfDay, int minute) { Calendar c = Calendar.getInstance(); //获取日期对象 c.set(Calendar.HOUR_OF_DAY, hourOfDay); //设置闹钟小时数 c.set(Calendar.MINUTE, minute); //设置闹钟分钟数 Intent intent = new Intent(PointerActivity.this,AlarmReceiver.class); //创建pendingIntent PendingIntent pendingIntent = PendingIntent.getBroadcast(PointerActivity.this,0X102, intent,0); //设置闹钟 alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); Toast.makeText(PointerActivity.this, "闹钟设置成功", Toast.LENGTH_SHORT).show(); } },hour,minute,true); timePickerDialog.show(); } }
package com.example.daka; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; public class AlarmActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //显示闹钟提醒框 new AlertDialog.Builder(AlarmActivity.this) .setTitle("闹钟") .setMessage("时间到了") .setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialogInterface, int which) { AlarmActivity.this.finish(); } }).create().show(); } }
package com.example.daka; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, AlarmActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <Button android:id="@+id/set_clock" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置闹钟"/> </LinearLayout>
原文连接:
https://www.jb51.net/article/262363.htm
标签:Calendar,APP,public,Intent,闹钟,import,之定,打卡,android From: https://www.cnblogs.com/cinan/p/17232185.html