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

1.制作一次性闹钟

2.制作周期闹钟

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

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

简单的思路:

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

android值制作简易的闹钟和通知栏信息_第1张图片



再来一个通知的思维导图

android值制作简易的闹钟和通知栏信息_第2张图片

代码如下,里面有详解

[html]  view plain  copy
  1. package com.example.cookie.android0723alarmone;  
  2.   
  3. import android.app.AlarmManager;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.app.TimePickerDialog;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.os.Bundle;  
  12. import android.support.v7.app.NotificationCompat;  
  13. import android.view.View;  
  14. import android.widget.TimePicker;  
  15.   
  16. import java.util.Calendar;  
  17.   
  18. public class MainActivity extends AppCompatActivity {  
  19.   
  20.     private AlarmManager am;  
  21.     private PendingIntent pendingIntent;  
  22.     private NotificationManager notificationManager;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         //获取闹钟管理器  
  29.         am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  30.         //获取通知管理器  
  31.         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  32.   
  33.     }  
  34.   
  35.     public void setAlarm(View view){  
  36.         Calendar calendar=Calendar.getInstance();  
  37.         int hour=calendar.get(Calendar.HOUR_OF_DAY);  
  38.         int minute=calendar.get(Calendar.MINUTE);  
  39.         //1.弹出提示框  
  40.         TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {  
  41.             @Override  
  42.             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  43.                 //2.获取闹钟时间  
  44.                 Calendar c=Calendar.getInstance();  
  45.                 c.set(Calendar.HOUR_OF_DAY,hourOfDay);  
  46.                 c.set(Calendar.MINUTE,minute);  
  47.                 //3.设置闹钟  
  48.                 PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.example.cookie.android0723alarmone.RING"),0);  
  49.                 am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);  
  50.             }  
  51.         },hour,minute,true);  
  52.   
  53. timePickerDialog.show();  
  54.     }  
  55.   
  56.     public void setAlarmRepeat(View view){  
  57.         Calendar calendar=Calendar.getInstance();  
  58.         int hour=calendar.get(Calendar.HOUR_OF_DAY);  
  59.         int minute=calendar.get(Calendar.MINUTE);  
  60.         //1.弹出对话框  
  61.         TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {  
  62.   
  63.   
  64.   
  65.             @Override  
  66.             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  67.                 //2.获取闹钟时间  
  68.                 Calendar c=Calendar.getInstance();  
  69.                 c.set(Calendar.HOUR_OF_DAY,hourOfDay);  
  70.                 c.set(Calendar.MINUTE,minute);  
  71.                 //3.设置闹钟  
  72.                 pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,new Intent("com.example.cookie.android0723alarmone.RING"),0);  
  73.                 am.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);  
  74.   
  75.             }  
  76.         },hour,minute,true);  
  77.   
  78.         timePickerDialog.show();  
  79.   
  80.     }  
  81.   
  82.     public void stopRepeat(View view){  
  83.         am.cancel(pendingIntent);  
  84.     }  
  85.   
  86.     public void send(View view){  
  87.         //实例化通知  
  88.         NotificationCompat.Builder builder=new NotificationCompat.Builder(this);  
  89.         builder.setContentTitle("提示");  
  90.         builder.setContentText("恭喜您,中奖了,请先汇款给某某激活您的账号");  
  91.         //设置图标  
  92.         builder.setSmallIcon(android.R.drawable.star_on);  
  93.         //使用默认的闪光灯,铃声  
  94.         builder.setDefaults(NotificationCompat.DEFAULT_ALL);  
  95.         Notification notification=builder.build();  
  96.         //发送通知  
  97.         notificationManager.notify(0x101,notification);  
  98.     }  
  99.   
  100. }  

RingReceiver广播接收者

[html]  view plain  copy
  1. package com.example.cookie.android0723alarmone;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. /**  
  9.  * Created by Administrator on 2017/7/23 0023.  
  10.  */  
  11.   
  12. public class RingReceiver extends BroadcastReceiver {  
  13.   
  14.     @Override  
  15.     public void onReceive(Context context, Intent intent) {  
  16.         if ("com.example.cookie.android0723alarmone.RING".equals(intent.getAction())){  
  17.             Log.i("test","响铃了!!!");  
  18.             //跳转到activity中  
  19.             Intent intent2=new Intent(context,RingActivity.class);  
  20.             intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  21.             context.startActivity(intent2);  
  22.         }  
  23.     }  
  24. }  

RingActivity闹钟播放

[html]  view plain  copy
  1. package com.example.cookie.android0723alarmone;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. /**  
  9.  * Created by Administrator on 2017/7/23 0023.  
  10.  */  
  11.   
  12. public class RingReceiver extends BroadcastReceiver {  
  13.   
  14.     @Override  
  15.     public void onReceive(Context context, Intent intent) {  
  16.         if ("com.example.cookie.android0723alarmone.RING".equals(intent.getAction())){  
  17.             Log.i("test","响铃了!!!");  
  18.             //跳转到activity中  
  19.             Intent intent2=new Intent(context,RingActivity.class);  
  20.             intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  21.             context.startActivity(intent2);  
  22.         }  
  23.     }  
  24. }  

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