Android 通知栏 Notification 基本使用

在Android操作系统中,状态栏通知Notification的运用是很常见的,比如我们收到短信,QQ消息等,在手机顶端通知栏就会显示一条图文消息来提示我们。Notification有很多的用法,比如最基本的即时消息的提示,更新应用或下载文件的进度条式的提示,用来显示长文本和一些自定义布局的折叠式的提示,Android5.0新增加的悬挂式的提示等。下面我们介绍一个最基本的Notification,了解大致的流程和设置属性。

建立Notification大致需要以下四步:

1.从系统服务中获得通知管理器NotificationManager对象
2.建立Notification
3.定义一个PendingIntent,将PendingIntent关联到Notification
4.通过通知管理器来发起通知

public class Demo6 extends AppCompatActivity {
 
    private Context mContext;
    private Bitmap LargeBitmap = null;
    private NotificationManager myManager = null;
    private Notification myNotification;
    private static final int NOTIFICATION_ID_1 = 1;
    private Button on, off;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo6);
 
        on = (Button) findViewById(R.id.on);
        off = (Button) findViewById(R.id.off);
        mContext = Demo6.this;
        //创建大图标的Bitmap
        LargeBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //1.从系统服务中获得通知管理器
        myManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 
        on.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
 
                //3.定义一个PendingIntent,点击Notification后启动一个Activity
                PendingIntent pi = PendingIntent.getActivity(
                        mContext,
                        100,
                        new Intent(mContext, DemoMain.class),
                        PendingIntent.FLAG_CANCEL_CURRENT
                );
 
                //2.通过Notification.Builder来创建通知
                Notification.Builder myBuilder = new Notification.Builder(mContext);
                myBuilder.setContentTitle("QQ")
                        .setContentText("这是内容")
                        .setSubText("这是补充小行内容")
                        .setTicker("您收到新的消息")
                        //设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(LargeBitmap)
                        //设置默认声音和震动
                        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                        .setAutoCancel(true)//点击后取消
                        .setWhen(System.currentTimeMillis())//设置通知时间
                        .setPriority(Notification.PRIORITY_HIGH)//高优先级
                        .setVisibility(Notification.VISIBILITY_PUBLIC)
                        //android5.0加入了一种新的模式Notification的显示等级,共有三种:
                        //VISIBILITY_PUBLIC  只有在没有锁屏时会显示通知
                        //VISIBILITY_PRIVATE 任何情况都会显示通知
                        //VISIBILITY_SECRET  在安全锁和没有锁屏的情况下显示通知
                        .setContentIntent(pi);  //3.关联PendingIntent
                myNotification = myBuilder.build();
                //4.通过通知管理器来发起通知,ID区分通知
                myManager.notify(NOTIFICATION_ID_1, myNotification);
            }
        });
 
        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //除了根据ID来取消通知,还可以调用cancelAll()关闭该应用产生的所有通知
                myManager.cancel(NOTIFICATION_ID_1);
            }
        });
 
 
    }
}

最基本的先介绍这些,以后会介绍其他自定义用法的通知!

 

 

 

 

 

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值