简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmManager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新的activity播放音乐。接着是一个反复闹钟,最后是一个简单的通知,具体代码如下:
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
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.support.v7.app.NotificationCompat;
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 setAlarm(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 timePicker, int i, int i1) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,i);
c.set(Calendar.MINUTE,i1);
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
//将来时态的跳转 ang eng ing ong
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
//时间一到,发送广播(闹钟响了)
//广播接受者中(跳转Activity)
// 跳转Activity,在这个Activity中播放音乐
}
},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);
//弹出时间对话框
TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,i);
c.set(Calendar.MINUTE,i1);
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
//将来时态的跳转 ang eng ing ong
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
// alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
//设置周期闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
//时间一到,发送广播(闹钟响了)
//广播接受者中(跳转Activity)
// 跳转Activity,在这个Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
public void cancelCycle(View view){
alarmManager.cancel(pendingIntent);
}
public void sendNotification(View view){
//实例化通知管理器
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setContentTitle("今日大事件");
builder.setContentText("骑士击败勇士夺冠");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setAutoCancel(true);
builder.setSmallIcon(android.R.drawable.ic_media_play);
builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0));
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x101,notification);
}
}
布局文件(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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="ghq.zking.com.ghq_android_28.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置闹钟(一次)"
android:onClick="setAlarm"
/>
<Button
android:layout_width="match_parent"
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="cancelCycle"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知"
android:onClick="sendNotification"
/>
</LinearLayout>
广播接受者代码:
public class RingReceived extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("ghq.zking.com.ghq_android_28_alarm_notification.RING".equals(intent.getAction())){
//跳转到Activity
Intent intent1=new Intent(context,RingActivity.class);
//给Intent设置标志位
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
闹钟播放activity代码:
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.love);
mediaPlayer.start();
}
public void stop(View view){
mediaPlayer.stop();
finish();
}
}
您可能感兴趣的文章:Android通过AlarmManager类实现简单闹钟功能Android实现简易闹钟功能简单实现Android闹钟程序 附源码Android编程实现闹钟的方法详解Android闹钟设置的解决方案Android手机闹钟用法实例Android编程闹钟设置方法详解Android编程实现PendingIntent控制多个闹钟的方法Android闹钟启动时间设置无效问题的解决方法Android实现闹钟小程序