自定义闹钟,通知

效果图:
这里写图片描述

实现代码:
主要MainActivty中的代码;

package com.example.android_28_alarm_notification;

import android.annotation.TargetApi;
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.os.Build;
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 setAlarmOne(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() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                //02.确定选择好的时间  i  i1
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);
                //04.时间一到,执行相对应的操作
                Intent intent=new Intent();
                intent.setAction("com.zking.g150825_android28_alarm_notification.RING");
                PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
                //03.设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
            }
        },minute,hour,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() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @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.zking.g150825_android28_alarm_notification.RING");
                pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,intent,0);

                //03.设置闹钟(一次)
//                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

                //03.设置闹钟(周期)
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
            }
        },hour,minute,true);
        timePickerDialog.show();

    }
    public void cancelAlarmCycle(View view){
        alarmManager.cancel(pendingIntent);
    }
}

跳转到的activty;

package com.example.android_28_alarm_notification;

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 savedIstanceState) {
        super.onCreate(savedIstanceState);
        setContentView(R.layout.activity_ring);

        //播放音乐
        mediaPlayer = MediaPlayer.create(this, R.raw.laojie);
        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("哈喽,该起床啦!");
        Notification notification=builder.build();
        //发送通知
        notificationManager.notify(0x103,notification);

        notification.flags = Notification.FLAG_AUTO_CANCEL;
    }

    public void stop(View view){
        mediaPlayer.stop();
        finish();
    }

}

广播中的代码 :

public class RingReceived extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.zking.g150825_android28_alarm_notification.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);

        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值