Android-闹钟(单次闹钟及周期闹钟),发送通知

时间:2022-07-18 16:44:24

首先,写个简单的布局文件:

0.1设置闹钟界面

<?xml version="1.0" encoding="utf-8"?>
<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="com.asus.android_28_alarm_notification.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="cancelAlarmCycle"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送通知"
        android:onClick="sendNotification"
        />
</LinearLayout>
0.2闹铃界面

<?xml version="1.0" encoding="utf-8"?>
<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: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="com.asus.android_28_alarm_notification.RingActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="懒猪,起床了!"
        android:textSize="20sp"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止"
        android:onClick="stop"
        />

</LinearLayout>

0.3写一个服务类

注:服务需要配置

	<receiver android:name=".RingReceived">
            <intent-filter>
                <action android:name="com.asus.android_28_alarm_notification"></action>
            </intent-filter>
        </receiver>


package com.asus.android_28_alarm_notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by ASUS on 2017/2/15.
 */

public class RingReceived extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.asus.android_28_alarm_notification".equals(intent.getAction())) {
            Log.i("test","闹钟响了");
            //跳转到Activity
            Intent intent1=new Intent(context,RingActivity.class);
            //getIntent设置标志位
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
    }
}



1.Android中单单设置闹钟比较简单:

注:闹钟管理者我设置的是全局变量:

    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 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 view, int hourOfDay, int minute) {
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);

                Intent intent=new Intent();
                intent.setAction("com.asus.android_28_alarm_notification");

                //将来时态的跳转
                PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0x00,intent,0);

                //设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
                //时间一到,发送广播
                //广播接受者(跳转Activity)
                //时间一到,跳转Activity,并在该Activity中播放音乐

            }
        },hour,minute,true);
        timePickerDialog.show();
    }

2.设置周期闹钟

注:pendingIntent是全局变量

 
private PendingIntent pendingIntent;

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 view, int hourOfDay, int minute) {
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);

                Intent intent=new Intent();
                intent.setAction("com.asus.android_28_alarm_notification");

                //将来时态的跳转
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0x00,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();
    }
3.结束周期闹钟

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


4.发送系统通知

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);

    }