activity_main.xml:
activity_ring.xml:
MainActivity:
package com.example.android_alarmandnotification;
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 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 openAlarm(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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar1.set(Calendar.MINUTE,minute);
Intent intent=new Intent();
intent.setAction("com.example.android_alarmandnotification");
//将来时态的跳转
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingIntent);
//时间一到就发广播
//广播接受者中,(跳转activity)
//在Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
//周期性闹钟
public void openAlarmCycle(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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar1.set(Calendar.MINUTE,minute);
Intent intent=new Intent();
intent.setAction("com.example.android_alarmandnotification");
//将来时态的跳转
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),5000, pendingIntent);
//时间一到就发广播
//广播接受者中,(跳转activity)
//在Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
public void CancleCycle(View view){
alarmManager.cancel(pendingIntent);
}
}
Ringactivity:
package com.example.android_alarmandnotification;
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 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 openAlarm(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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar1.set(Calendar.MINUTE,minute);
Intent intent=new Intent();
intent.setAction("com.example.android_alarmandnotification");
//将来时态的跳转
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingIntent);
//时间一到就发广播
//广播接受者中,(跳转activity)
//在Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
//周期性闹钟
public void openAlarmCycle(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 calendar1=Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar1.set(Calendar.MINUTE,minute);
Intent intent=new Intent();
intent.setAction("com.example.android_alarmandnotification");
//将来时态的跳转
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
// alarmManager.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),5000, pendingIntent);
//时间一到就发广播
//广播接受者中,(跳转activity)
//在Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
public void CancleCycle(View view){
alarmManager.cancel(pendingIntent);
}
}
RingReceived:
package com.example.android_alarmandnotification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/2/14.
*/
public class RingReceived extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if ("com.example.android_alarmandnotification".equals(intent.getAction())){
Log.i("test","闹钟响了");
//跳转到Activity
Intent intent1=new Intent(context,RingActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
`1