zoukankan      html  css  js  c++  java
  • Android notifications通知栏的使用

    app发送通知消息到通知栏中的关键代码和点击事件:

    package com.example.notifications;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        public static final int notifi_id=0x1;
        public static final int notifi_id2=0x2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
        }
        
        public void sendNotifi(View v){
            NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setContentTitle("您有一条新消息");
            builder.setContentText("新年快乐!");
            builder.setTicker("新消息");
            builder.setDefaults(Notification.DEFAULT_ALL);
            //builder.setAutoCancel(true);
            Intent intent=new Intent(this,Second.class);
            PendingIntent pi=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pi);
            //builder.setOngoing(true);//常驻通知
            //创建一个对象通知
            Notification n=builder.build();
            //获取系统的通知管理器,然后发送通知
            NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(notifi_id,n);
        }
    
        public void sendNotifi2(View v){
            NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.drawable.ic_launcher);
            /*builder.setContentTitle("您有一条新消息");
            builder.setContentText("新年快乐!");*/
            
            //设置大图样式
            NotificationCompat.InboxStyle style=new NotificationCompat.InboxStyle();
            style.setBigContentTitle("大通知");
            style.addLine("冬天");
            style.addLine("下雪了");
            builder.setStyle(style);
            Notification n=builder.build();
            NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(notifi_id2,n);
        }
        
    }
  • 相关阅读:
    Quartz定时调度CronTrigger时间配置格式说明与实例
    JAVA中堆和栈的区别
    s:iterator循环输出数字
    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
    FilenameFilter总结
    Oracle sql"NOT IN"语句优化,查询A表有、B表没有的数据
    Java并发教程(Oracle官方资料)
    做考试系统用到的关于onbeforeunload一些兼容性问题
    Java集合类ArrayList循环中删除特定元素
    Log4j写入数据库详解
  • 原文地址:https://www.cnblogs.com/hyyweb/p/5145475.html
Copyright © 2011-2022 走看看