闹钟是手机中非常常用的应用,当然我们也可以实现它
在闹钟中分为一次闹钟和周期闹钟,顾名思义,一次闹钟就会响一次后就失效,周期闹钟就是隔一段时间后会再次响铃。
今天就给大家分享两种闹钟的基本代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.android27_zhangkai_alarm_notification.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(一次)" android:onClick="setAlarmOnce" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(周期)" android:onClick="setAlarmRepeat" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消周期闹钟" android:onClick="cancelAlarmRepeat" ></Button></LinearLayout>
- 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
- 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
MainActivity.Java
package com.example.android27_zhangkai_alarm_notification;import android.app.AlarmManager;import android.app.PendingIntent;import android.app.TimePickerDialog;import android.content.Context;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TimePicker;import java.util.Calendar;public class MainActivity extends AppCompatActivity { private AlarmManager am; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); } public void setAlarmOnce(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); PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.example.android27_zhangkai_alarm_notification.RING"),0); am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent); } },hour,minute,true); timePickerDialog.show(); } public void setAlarmRepeat(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); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0x102, new Intent("com.zking.administrator.g160628_android29_alarm_notification.RING"), 0); am.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent); } },hour,minute,true); timePickerDialog.show(); } public void cancelAlarmRepeat(View view){ am.cancel(pendingIntent); }}
- 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
- 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
因为设置闹钟的页面和闹钟响铃的页面为两个Activity,所以我在此运用了广播的技术,当闹钟时间到了,触发服务发送广播给响铃的页面,然后开始响铃。
服务页面:
package com.example.android27_zhangkai_alarm_notification;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;/** * Created by Administrator on 2017/7/23 0023. */public class RingReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.android27_zhangkai_alarm_notification.RING".equals(intent.getAction())){ Intent intent1=new Intent(context,RingActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
响铃页面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="懒PIG,该出窝了!!!" android:textSize="40sp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onClick="stop" /></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
package com.example.android27_zhangkai_alarm_notification;import android.media.MediaPlayer;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;/** * Created by Administrator on 2017/7/23 0023. */public class RingActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ring); mediaPlayer = MediaPlayer.create(this, R.raw.c); mediaPlayer.start(); } public void stop(View view){ mediaPlayer.stop(); finish(); }}
- 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
- 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
记得要在配置页面配置服务哟
<receiver android:name=".RingReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.example.android27_zhangkai_alarm_notification.RING"></action> </intent-filter> </receiver>