Android提醒功能小结

Android中的提醒功能主要由较为简单的Toast和比较复杂的Notification提供

Toast

Toast是显示信息的一种机制,无焦点,显示信息少,时间短,自动消失,但使用方式也最为简单。
默认Toast
Toast.makeText(this,"this is a Tosat",Toast.LENGTH_SHORT);

第一个参数为显示的容器,第二个为显示内容,第三个为显示的时间,其中时间分别有短时Toast.LENGTH_SHORT与长时Toast.LENGTH_LONG两个选项。

简单的自定义Toast
Toast toast = Toast.makeText(Toast_Notification.this,"this is user-defined Toast",Toast.LENGTH_LONG);
//设置Toast位置
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout linearLayout = (LinearLayout)toast.getView();
ImageView image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.mydearst);
linearLayout.addView(image);
toast.setView(linearLayout);
toast.show();

Notification

Notification是一种有关提醒的控件,不仅可显示信息,还可通过振动、提示音、状态栏等方式进行提醒,通常与NotificationManager一起使用。
Notification的基本操作
  1. 内容:setContentText(“通知内容”)
  2. 标题:setContentTitle(“通知标题”)
  3. 大图标:setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.xxx))
  4. 小图标:setSmallIcon(R.drawable.xxx)
Notification与NotificationManager的通知设置。
  1. 获取通知栏管理器NotificationManager
NotificationManager mNotificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  1. 实例化通知栏构造器NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(Toast_Notification.this);
  1. 设置Notification.Builder
builder.setContentText("通知内容")
        .setContentTitle("通知标题")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.mydearst))
        .setWhen(System.currentTimeMillis());
  1. 通过 builder.build() 方法生成 Notification 对象,并发送通知
//通知ID号,Notification对象
Notification notify = builder.build();
mNotificationManager.notify(0,notify);
Notifiation与应用的交互
Intent intent = new Intent(Toast_Notification.this,MainActivity.class);
//PendingIntent.getActivity(context,requestCode,perIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//若要打开不同的应用,需要使用不同的请求码requestCode
PendingIntent pendingIntent = PendingIntent.getActivity(Toast_Notification.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
......
builder..setContentIntent(pendingIntent);
Notification的更新

重新发送相同ID号的通知即可更新相关通知信息

Notification的取消
  1. 移走或刷新通知栏
  2. 设置了FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
  3. 通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
  4. 通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
  5. 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
Notification的通知效果
默认设置

Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过 setDefaults(int defualts) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效。

//例:builder.setDefaults(int defualts)
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE

//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND

//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS

//添加上述三种默认提醒效果
Notification.DEFAULT_ALL
flag效果设置
//例:notify.flags = Notification.FLAG_SHOW_LIGHTS
//三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_SHOW_LIGHTS

//发起正在运行事件(活动中)
Notification.FLAG_ONGOING_EVENT

//让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_INSISTENT

//发起Notification后,铃声和震动均只执行一次
Notification.FLAG_ONLY_ALERT_ONCE

//用户单击通知后自动消失
Notification.FLAG_AUTO_CANCEL

//只有调用NotificationManager.cancel()时才会清除
Notification.FLAG_NO_CLEAR

//表示正在运行的服务
Notification.FLAG_FOREGROUND_SERVICE
自定义设置
//声音,调用自己提供的铃声
builder.setSound(......);
//振动,参数为long数组
long[] vibrate = {0,50,100,150};
builder.setVibrate(vibrate);
//灯光,ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
builder.setLights(ledARGB, ledOnMS, ledOffMS);
Notification notify = builder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值