Android实现闹钟设置并全局广播的代码和布局
时间: 2024-02-18 21:06:09 浏览: 94
以下是一个简单的Android闹钟设置和全局广播的代码示例,包括布局文件和Java代码:
布局文件:activity_main.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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Alarm" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Alarm" />
</LinearLayout>
```
Java代码:MainActivity.java
```java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
TimePicker timePicker;
TextView updateText;
Context context;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
timePicker = (TimePicker) findViewById(R.id.timePicker);
updateText = (TextView) findViewById(R.id.textView);
final Calendar calendar = Calendar.getInstance();
Button start_alarm = (Button) findViewById(R.id.button);
start_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
String hour_string = String.valueOf(hour);
String minute_string = String.valueOf(minute);
if (hour > 12) {
hour_string = String.valueOf(hour - 12);
}
if (minute < 10) {
minute_string = "0" + String.valueOf(minute);
}
setAlarmText("Alarm set to: " + hour_string + ":" + minute_string);
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
});
}
public void setAlarmText(String alarmText) {
updateText.setText(alarmText);
}
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Wake up!", Toast.LENGTH_LONG).show();
}
}
}
```
在此示例中,我们使用了一个 `TimePicker` 控件让用户选择时间,然后使用 `AlarmManager` 在指定时间触发一个广播。我们也创建了一个广播接收器 `AlarmReceiver`,以便在指定时间触发时显示一个 `Toast` 消息。
请注意,这只是一个简单的示例,并且可能需要进行更多的修改和改进才能符合您的实际需求。
阅读全文