闹钟是每部手机必备的一个软件,今天就来分享一下。这个demo分一次性闹钟,周期性闹钟。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.an_alarm.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟(一次)"
android:onClick="setAlarmOne"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟(周期)"
android:onClick="setAlarmCycle"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消周期闹钟"
android:onClick="cancelAlarmCycle"
/>
LinearLayout>
activity:
package com.example.an_alarm;
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.util.Log;
import android.view.View;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取闹钟管理者
alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
//设置一次
public void setAlarmOne(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour= calendar.get(Calendar.DAY_OF_MONTH);
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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay,minute);
calendar1.set(Calendar.MINUTE,minute);
Log.i("test","调了1");
//.当之间一到,将执行的响应
Intent intent=new Intent();
intent.setAction("com.zking.g150831_android28_alarm_notification.RING");
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//一次
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(),pendingIntent);
Log.i("test","调了2");
}
},hour,minute,true);
timePickerDialog.show();
}
//周期闹钟
public void setAlarmCycle(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour= calendar.get(Calendar.DAY_OF_MONTH);
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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay,minute);
calendar1.set(Calendar.MINUTE,minute);
Log.i("test","调了3");
//.当之间一到,将执行的响应
Intent intent=new Intent();
intent.setAction("com.zking.g150831_android28_alarm_notification.RING");
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x103,intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(),10000, pendingIntent);
Log.i("test","调了4");
}
},hour,minute,true);
timePickerDialog.show();
}
//取消
public void cancelAlarmCycle(View view){
alarmManager.cancel( pendingIntent);
}
}
通过广播发送,必然接收:
package com.example.an_alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/2/22.
*/
public class Ringrescevied extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("com.zking.g150831_android28_alarm_notification.RING".equals(intent.getAction())){
Log.i("test","收到广播了");
//跳转到另一个activity intent
Intent intent1=new Intent(context,Ring.class);
//给Intent 设置标志位flag
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Intent
context.startActivity(intent1);
}
}
}
必要记得配置清单文件:
<receiver android:name=".Ringrescevied">
<intent-filter>
<action android:name="com.zking.g150831_android28_alarm_notification.RING">action>
intent-filter>
receiver>
跳转activity播放音乐并通知:
package com.example.an_alarm;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
public class Ring extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
//播放音乐
mediaPlayer = MediaPlayer.create(this, R.raw.young);
mediaPlayer.start();
//显示通知栏 Builder 构建器
NotificationCompat.Builder notificationCompat=new NotificationCompat.Builder(this);
//设置参数
notificationCompat.setDefaults(Notification.DEFAULT_ALL);
//标题
notificationCompat.setContentTitle("信息来了");
//内容
notificationCompat.setContentText("看看吧");
//得到通知
Notification notificationCompat1=notificationCompat.build();
// 得到通知管理器
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(0x11,notificationCompat1);
//notificationCompat.setContent(,Intent.class);
}
//关闭
public void close(View view){
mediaPlayer.stop();
finish();
}
}
其对应的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_ring"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.an_alarm.Ring">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="主人,起床啦"
android:textSize="30sp"
/>
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/analogClock" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭"
android:onClick="close"
/>
LinearLayout>