Realization of Simple Alarm Clock Function Based on Alarmmanager
- 2021-09-12 02:20:27
- OfStack
In this paper, we share the specific code of Alarmmanager to realize simple alarm clock function for your reference. The specific contents are as follows
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Start the alarm clock "
android:id="@+id/button"
android:onClick="startAlarmClick"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Set the alarm clock "
android:id="@+id/button2"
android:layout_below="@+id/button"
android:onClick="startSetAlarmClick"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
MainActivity.java
package com.example.haige.alarmmanager;
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;
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*
Start the alarm clock
*/
public void startAlarmClick(View view)
{
// Get the alarm clock service of the system
AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Time to trigger alarm clock (milliseconds)
long triggerTime= System.currentTimeMillis()+3000;
Intent intent=new Intent(this,Alarmctivity.class);
PendingIntent op=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
// Start 1 The second time will only be executed 1 A second alarm clock
am.set(AlarmManager.RTC,triggerTime,op);
// // Repeat the alarm clock at a specified time
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
/*
Set the alarm clock
*/
public void startSetAlarmClick(View view)
{
// Get the alarm clock service of the system
AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Time to trigger alarm clock (milliseconds)
long triggerTime= System.currentTimeMillis()+3000;
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent op=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
// Start 1 The second time will only be executed 1 A second alarm clock
am.set(AlarmManager.RTC,triggerTime,op);
// // Repeat the alarm clock at a specified time
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
}
activity_alarmctivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.haige.alarmmanager.Alarmctivity">
</RelativeLayout>
Alarmctivity.java
package com.example.haige.alarmmanager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import java.io.IOException;
public class Alarmctivity extends Activity
{
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarmctivity);
mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.parse("/storage/sdcard1/kugou/ Hearing / Nan Quan Mama - You're not like her .mp3"));
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
alarmOialog();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mp!=null)
{
if(mp.isPlaying())
{
mp.stop();
}
mp.release();
}
}
public void alarmOialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(" Big brother, big sister called you to get up! ");
builder.setPositiveButton(" Come again 1 Times ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alarm();
finish();
}
});
builder.setNegativeButton(" Stop ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();// Close a window
}
});
builder.show();
}
private void alarm() {
// Get the alarm clock service of the system
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Time to trigger alarm clock (milliseconds)
long triggerTime = System.currentTimeMillis() + 5000;
Intent intent = new Intent(this, Alarmctivity.class);
PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Start 1 The second time will only be executed 1 A second alarm clock
am.set(AlarmManager.RTC, triggerTime, op);
// Repeat the alarm clock at a specified time
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
}
Broadcast receiver AlarmReceiver. java
package com.example.haige.alarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context," Get up, get up! ",Toast.LENGTH_SHORT).show();
}
}