Android 通知栏的简单使用含API26以上

通知栏使用

API26以下


  1. a) NotificationManager
    i. 获取方法:
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)
    b) Notification
    i. 获取方法:new NotificationCompat.Builder(Context).build();得到
  2. 使用
    a) 常规使用
    i. 建立NotificationManager和Notification类
    ii. notification.setXX添加功能
    iii. manager.notify(id,notification)来使用通知
    b) 基本功能
    i. .setContentTitle:标题
    ii. .setContentText:内容
    iii. .setSmallIcon:小图标
    iv. .setLargeIcon(getResource,R.XX.XX):大图标
    v. .setWhen:发出通知的时间,单位为毫秒
    c) 点击事件
    i. 新建Intent,加入属性
    ii. 实例化PendingIntent类
  3. 可用该类的静态方法getActivity/getBroadcast/getService来得到实例
  4. 参数
    a) Context
    b) 通常为0
    c) Intent对象
    d) 确定PendingIntent行为,通常传入0
    iii. .setContentIntent(PendingIntent实例)
    d) 取消
    i. .setAutoCancel()
    ii. 点击活动中manage.cancel(id)
    e) 高级功能
    i. .setSound(Uri.FromFile()):声音
    ii. .setVibrate(long[])
    iii. .setLights(Color.XX,亮起时间,熄灭时间)
    iv. .setDefault(NotificationCompat.DEFAULT_ALL)
    v. .setStyle(new NotificationCompat.XXXStyle):图片要转为BitMap对象(BitMapFactory.decodeResource)
    vi. .setPrority()
  5. PRIORITY_MIN:下拉状态栏才显示
  6. PRIORITY_LOW:缩小,靠后
  7. PRIORITY_DEFAULT:默认
  8. PRIORITY_HIGH:放大,靠前
  9. PRIORITY_MAX:立即看到

API26以上(安卓O以上)

  1. 新加类:NotificationChannel
    a) 作用:用于管理一组通知
    b) 实例化
    i. new NotificationChannel(id,name,importance)
    ii. importance用Notification的IMPORTANCE_XX常量
    iii. 代替了.setPriority(被废弃)
    c) 功能
    i. setLight,setLightColor,setVibration,setVibrationPattern等系统调用功能
    d) 需NotificationManager调用createNotificationChannel
  2. 其他变化
    a) Notification实例化的时候需要加一个参数:channel_id
    b) Notification大量方法被废弃
    以下是测试代码,若需兼容api26以下版本需使用 if (Build.VERSION.SDK_INT >= 26)进行兼容
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String id = "channel_1";
        String description = "123";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);
        final Notification notification = new Notification.Builder(this, id)
                .setContentTitle("Title")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentText("测试内容")
                .setAutoCancel(true)
                .build();
        Button button = (Button)findViewById(R.id.sendNotification_Button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNotificationManager.notify(1, notification);
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值