android值制作简易的闹钟和通知栏信息

1.制作一次性闹钟

2.制作周期闹钟

3.制作通知信息的弹出框

首先来一个闹钟思维导图吧

简单的思路:

设置(MainActivity)----------->广播接受者(RingReceiver)------------>响铃(RingActivity)---------->设置(MainAcitvity)




再来一个通知的思维导图


代码如下,里面有详解

package com.example.cookie.android0723alarmone;

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 am;
    private PendingIntent pendingIntent;
    private NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取闹钟管理器
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        //获取通知管理器
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    }

    public void setAlarm(View view){
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        //1.弹出提示框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                //2.获取闹钟时间
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);
                //3.设置闹钟
                PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.example.cookie.android0723alarmone.RING"),0);
                am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
            }
        },hour,minute,true);

timePickerDialog.show();
    }

    public void setAlarmRepeat(View view){
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        //1.弹出对话框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {



            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                //2.获取闹钟时间
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);
                //3.设置闹钟
                pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.example.cookie.android0723alarmone.RING"),0);
                am.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);

            }
        },hour,minute,true);

        timePickerDialog.show();

    }

    public void stopRepeat(View view){
        am.cancel(pendingIntent);
    }

    public void send(View view){
        //实例化通知
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setContentTitle("提示");
        builder.setContentText("恭喜您,中奖了,请先汇款给某某激活您的账号");
        //设置图标
        builder.setSmallIcon(android.R.drawable.star_on);
        //使用默认的闪光灯,铃声
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        Notification notification=builder.build();
        //发送通知
        notificationManager.notify(0x101,notification);
    }

}

RingReceiver广播接收者

package com.example.cookie.android0723alarmone;

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

/**
 * Created by Administrator on 2017/7/23 0023.
 */

public class RingReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.example.cookie.android0723alarmone.RING".equals(intent.getAction())){
            Log.i("test","响铃了!!!");
            //跳转到activity中
            Intent intent2=new Intent(context,RingActivity.class);
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }
    }
}

RingActivity闹钟播放

package com.example.cookie.android0723alarmone;

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

/**
 * Created by Administrator on 2017/7/23 0023.
 */

public class RingReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.example.cookie.android0723alarmone.RING".equals(intent.getAction())){
            Log.i("test","响铃了!!!");
            //跳转到activity中
            Intent intent2=new Intent(context,RingActivity.class);
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }
    }
}

xml代码就不写了,还要记得去配置哈。。。


你可能感兴趣的:(android值制作简易的闹钟和通知栏信息)