前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[android] notification入门

[android] notification入门

作者头像
唯一Chat
发布2019-09-10 11:59:22
4950
发布2019-09-10 11:59:22
举报
文章被收录于专栏:陶士涵的菜地

通知栏,对话框,Toast是我们接触的三个提示框,通知栏是在系统的应用com.adnroid.systemui当中的

接触的几个Manger,getSystemService()方法得到的,参数:

ACTIVITY_SERVICE,LAYOUT_INFLATER_SERVIC,TELEPHONY_SERVICE,

获取NotificationManager对象,通过getSystemService()方法,参数:NOTIFICATION_SERVICE

调用NotificationManager对象的notify()方法,发送通知,参数:id是0,Notification对象

获取Builder对象,通过new Notification.Builder()

调用Builder对象的setContentTitle() setContentText() setSmallIcon() setLargeIcon()

setLargeIcon的参数:Bitmap对象,BitmapFactory.decodeResource(),参数:getResoure(),资源id

调用Builder对象的build()方法,得到Notification对象

此时会报一些错误,最低兼容版本是11,我们直接加一个注释屏蔽掉错误”NewApi”

在低版本的手机上,上面的代码会出错

获取Notification对象,通过new出来,参数:资源id,文本,时间

调用Notification对象的setLastEventInfo()方法,设置最新消息,参数:上下文,文本,PendingIntent对象

设置Notification对象的flags属性为Notification.FLAG_AUTO_CANCEL 自动关闭

PendingIntent对象,通过PendingIntent的getActivity()方法,获取到PendingIntent对象

6.0 版本移除了Notification.setLatestEventInfo()方法

流氓软件会使用这来弹广告,我们可以进系统应用设置,勾掉显示通知

代码语言:javascript
复制
package com.tsh.tshnotification;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //显示通知栏
    @SuppressLint("NewApi")
    public void click(View v){
        NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder=new Notification.Builder(this);
        Notification notification=builder.setContentTitle("提示")
                .setContentText("我是陶士涵")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                .build();
                
        nm.notify(0, notification);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档