以下是我利用AlarmManager+BroadcastReceiver实现闹钟的功能,以及通过广播来实现通知,这里我的闹铃是存放在内存raw中,并不需要在清单文件中获取权限
activity_main.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.myapp.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:onClick="setAlarmCycle"
android:text="设置闹钟(周期)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消周期闹钟"
android:onClick="cancelAlarmCycle"
/>
LinearLayout>
MainActivity.java
package com.example.myapp;
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 PendingIntent pendingIntent;
private AlarmManager alarmManager;
@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.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) {
//2 确定选择好的时间 hourOfDay minut
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
//04.时间一到,执行相对应的操作
Intent intent=new Intent();
intent.setAction("com.example.myapp.RING");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//03.设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
//设置闹钟(周期)
public void setAlarmCycle(View view){
//获取当前系统的时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//01.弹出时间对话框(选择时间)
TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//02.确定选择好的时间 i i1
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,i);
c.set(Calendar.MINUTE,i1);
//04.时间一到,执行相对应的操作
Intent intent=new Intent();
intent.setAction("com.example.myapp.RING");
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,intent,0);
//03.设置闹钟(周期)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
///取消闹钟
public void cancelAlarmCycle(View view){
alarmManager.cancel(pendingIntent);
}
}
activity_ring.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.myapp.RingActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="you are my love!!!"
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="stop"
/>
LinearLayout>
RingActivity.java
package com.example.myapp;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
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 RingActivity 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.yang);
mediaPlayer.start();
//实例化通知管理器
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentTitle("闹钟响了");
builder.setSmallIcon(android.R.drawable.ic_lock_idle_alarm);
builder.setContentText("记得在早上9点之前吃早餐哦");
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x103,notification);
}
public void stop(View view){
mediaPlayer.stop();
finish();
}
}
广播
RingReceived.java
package com.example.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
/**
* Created by Administrator on 2017/2/22.
*/
public class RingReceived extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.example.myapp.RING".equals(intent.getAction())){
Log.i("test","闹钟响了");
//跳转到Activity
Intent intent1=new Intent(context,RingActivity.class);
//广播跳转到activity.必须给intent设置标志位flags
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
注意音乐时放在raw中