服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 基于Alarmmanager实现简单闹钟功能

基于Alarmmanager实现简单闹钟功能

2022-03-06 20:30潇潇雨歇_ Android

这篇文章主要为大家详细介绍了基于Alarmmanager实现简单闹钟功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Alarmmanager实现简单闹钟功能的具体代码,供大家参考,具体内容如下

代码:

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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="启动闹钟"
    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="设置闹钟"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:onClick="startSetAlarmClick"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true" />
 
</RelativeLayout>

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
  }
 
  /*
  启动闹钟
   */
  public void startAlarmClick(View view)
  {
    //获取系统的闹钟服务
    AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //触发闹钟的时间(毫秒)
    long triggerTime= System.currentTimeMillis()+3000;
    Intent intent=new Intent(this,Alarmctivity.class);
    PendingIntent op=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    //启动一次只会执行一次的闹钟
    am.set(AlarmManager.RTC,triggerTime,op);
//    //指定时间重复执行闹钟
//    am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
  }
 
  /*
  设置闹钟
   */
  public void startSetAlarmClick(View view)
  {
    //获取系统的闹钟服务
    AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //触发闹钟的时间(毫秒)
    long triggerTime= System.currentTimeMillis()+3000;
    Intent intent=new Intent(this,AlarmReceiver.class);
    PendingIntent op=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    //启动一次只会执行一次的闹钟
    am.set(AlarmManager.RTC,triggerTime,op);
//    //指定时间重复执行闹钟
//    am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
  }
}

activity_alarmctivity.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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/听力/南拳妈妈-你不像她.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("大哥,大妹子喊你起床啦!");
    builder.setPositiveButton("再来一次", new DialogInterface.OnClickListener() {
 
      @Override
      public void onClick(DialogInterface dialogInterface, int i) {
        alarm();
        finish();
      }
    });
 
    builder.setNegativeButton("停止", new DialogInterface.OnClickListener() {
 
      @Override
      public void onClick(DialogInterface dialogInterface, int i) {
        finish();//关闭窗口
      }
    });
    builder.show();
  }
 
  private void alarm() {
    //获取系统的闹钟服务
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //触发闹钟的时间(毫秒)
    long triggerTime = System.currentTimeMillis() + 5000;
    Intent intent = new Intent(this, Alarmctivity.class);
    PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //启动一次只会执行一次的闹钟
    am.set(AlarmManager.RTC, triggerTime, op);
    //指定时间重复执行闹钟
//    am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
  }
 
}

广播接收器AlarmReceiver.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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,"起床啦,起床啦!",Toast.LENGTH_SHORT).show();
  }
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/linhaiyun_ytdx/article/details/52512402

延伸 · 阅读

精彩推荐