android实现具有日程提醒功能的代码和布局文件
时间: 2024-02-27 11:52:01 浏览: 91
以下是一个简单的实现具有日程提醒功能的 Android 应用程序的代码和布局文件:
布局文件(schedule_layout.xml):
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Schedule Reminder"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"/>
<EditText
android:id="@+id/event_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your event here"
android:padding="16dp"/>
<EditText
android:id="@+id/date_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter date (MM/DD/YYYY)"
android:padding="16dp"/>
<EditText
android:id="@+id/time_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter time (HH:MM AM/PM)"
android:padding="16dp"/>
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Reminder"
android:layout_gravity="center"
android:padding="16dp"/>
</LinearLayout>
```
Java 代码(ScheduleActivity.java):
```java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class ScheduleActivity extends AppCompatActivity {
private EditText mEventEditText;
private EditText mDateEditText;
private EditText mTimeEditText;
private Button mAddButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_layout);
mEventEditText = findViewById(R.id.event_edittext);
mDateEditText = findViewById(R.id.date_edittext);
mTimeEditText = findViewById(R.id.time_edittext);
mAddButton = findViewById(R.id.add_button);
mAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String event = mEventEditText.getText().toString().trim();
String date = mDateEditText.getText().toString().trim();
String time = mTimeEditText.getText().toString().trim();
if (TextUtils.isEmpty(event)) {
Toast.makeText(ScheduleActivity.this, "Please enter an event", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(date)) {
Toast.makeText(ScheduleActivity.this, "Please enter a date", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(time)) {
Toast.makeText(ScheduleActivity.this, "Please enter a time", Toast.LENGTH_SHORT).show();
return;
}
String dateTime = date + " " + time;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date reminderDate = null;
try {
reminderDate = dateFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(ScheduleActivity.this, "Invalid date/time format", Toast.LENGTH_SHORT).show();
return;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(reminderDate);
Intent intent = new Intent(ScheduleActivity.this, ReminderBroadcastReceiver.class);
intent.putExtra("event", event);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ScheduleActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(ScheduleActivity.this, "Reminder set for " + DateFormat.getDateTimeInstance().format(calendar.getTime()), Toast.LENGTH_LONG).show();
}
});
}
}
```
此示例使用 AlarmManager 和 BroadcastReceiver 来设置提醒。您需要创建一个 BroadcastReceiver 类(在这个示例中称为 ReminderBroadcastReceiver),并在 AndroidManifest.xml 文件中声明它。您可以在 BroadcastReceiver 中定义提醒的外观和行为。在 AndroidManifest.xml 文件中,您还需要添加相应的权限:
```xml
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".ReminderBroadcastReceiver"/>
```
以下是 ReminderBroadcastReceiver 的示例代码:
```java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class ReminderBroadcastReceiver extends BroadcastReceiver {
private static final String CHANNEL_ID = "CHANNEL_ID";
private static final int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
String event = intent.getStringExtra("event");
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Reminder: " + event)
.setContentText("Your scheduled event is happening now!")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(soundUri)
.setVibrate(new long[] {0, 1000, 1000, 1000})
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
```
这个 BroadcastReceiver 在接收到广播时创建一个通知。您可以根据需要自定义通知的外观和行为。
阅读全文