安卓实现闹钟单次和周期性效果(下拉通知)

实现效果截图:

--------》选择时间

安卓实现闹钟单次和周期性效果(下拉通知)_第1张图片



-------》到时实现闹铃:

安卓实现闹钟单次和周期性效果(下拉通知)_第2张图片



安卓实现闹铃,需用到广播接收者,广播跳转到Activity(必须给Intent设置标志位Flags),

同时下拉菜单栏也会通知到时闹铃。


Activity代码:

package com.example.android_vedioplayer_camera_recorder;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * Created by Administrator on 2017/2/22.
 */
public class AlarmActivity extends AppCompatActivity {

    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);

        //实例化闹钟管理器
        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() {
            @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.android_vedioplayer_camera_recorder.RING");
                PendingIntent pendingIntent=PendingIntent.getBroadcast(AlarmActivity.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.android_vedioplayer_camera_recorder.RING");
                pendingIntent = PendingIntent.getBroadcast(AlarmActivity.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);
    }

}



广播接收类代码:

package com.example.android_vedioplayer_camera_recorder;

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

/**
 * Created by Administrator on 2017/2/22.
 * 闹铃广播接受类
 */

public class RingReceived extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("test","======="+intent.getAction());
        if("com.example.android_vedioplayer_camera_recorder.RING".equals(intent.getAction())){
            //跳转到Activity
            Intent intent1=new Intent(context,RingmusicActivity.class);
            //广播跳转到Activity(必须给Intent设置标志位Flags)
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
    }
}



闹钟到时响铃并播放音乐:

package com.example.android_vedioplayer_camera_recorder;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;

/**
 * Created by Administrator on 2017/2/22.
 */
public class RingmusicActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringmusic);

        //播放音乐
        mediaPlayer = MediaPlayer.create(this, R.raw.love);
        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("阿拉蕾,你妈叫你起床吃早餐啦");
        //builder.setAutoCancel(true);
        //建立通知
        Notification notification=builder.build();
        //发送通知
        notificationManager.notify(0x103,notification);
    }

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


layout.xml代码:

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: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"
    >

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



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: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"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="喂喂喂,你是谁,你是不是动画片里的阿拉蕾!起床啦"
        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>


清单文件代码:


<activity android:name=".AlarmActivity">

activity>

<receiver android:name=".RingReceived">
    <intent-filter>
        <action android:name="com.example.android_vedioplayer_camera_recorder.RING" />
    intent-filter>
receiver>

<activity android:name=".RingmusicActivity" />


你可能感兴趣的:(编程)