您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
            通知栏时出现在手机顶部的消息提醒机制,用户打开之后会显示通知信息,还可以与用户交互,并处理用户选择事件。
    一:创建
(1)得到NotificationManeger对象
// 得到NotificationManager对象
        NotificationManager manager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE); | 
(2)实例化Notification对象
// 初始化Notification对象
        Notification notification = new Notification(); | 
(3)设置Notification对象属性
// 设置显示在手机最上边的状态栏的图标
        notification.icon = R.drawable.ic_launcher;
        // 当当前的notification被放到状态栏上的那一刻,给出的提示
        notification.tickerText = "注意"; | 
(4)给出用户单击之后的响应动作
// 给出跳转信息
        Intent intent = new Intent(this, Info.class);
        // 用户单击时触发跳转
        PendingIntent pending = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT); | 
    (5)给出点击状态栏的图标出现的提示信息设置,参数:(Activity对象,标题信息,文本信息,PendingIntent对象)
notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pending);  | 
    (6)使用NotificationManeger对象的notify(通知ID,Notification对象)方法发出通知。(通知ID为以后的取消通知做准备)
manager.notify(1, notification);  | 
    二:具体应用
    功能:给出通知栏,用户单击之后触发弹出提示框的动作
◆主Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 得到NotificationManager对象
    NotificationManager manager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // 初始化Notification对象
    Notification notification = new Notification();
    // 设置显示在手机最上边的状态栏的图标
    notification.icon = R.drawable.ic_launcher;
    // 当当前的notification被放到状态栏上的那一刻,给出的提示
    notification.tickerText = "注意";
    // 添加声音提示
    // notification.defaults = Notification.DEFAULT_SOUND;
    // audioStreamType的值必须AudioManager中的值,代表着响铃的模式
    // notification.audioStreamType =
    // android.media.AudioManager.ADJUST_LOWER;
    // 给出跳转信息
    Intent intent = new Intent(this, Info.class);
    // 用户单击时触发跳转
    PendingIntent pending = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    // 点击状态栏的图标出现的提示信息设置(Activity对象,标题信息,文本信息,PendingIntent对象)
    notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pending);
    // 启动通知
    manager.notify(1, notification);
} | 
◆处理信息Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);
    //提示框
    AlertDialog.Builder dia = (Builder) new AlertDialog.Builder(this);
    dia.setTitle("提示");
    dia.setMessage("我是通知栏的提示信息");
    dia.setPositiveButton("确定", new OnClickListener() {
                                                            
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(Info.this, "您点击了确定按钮", Toast.LENGTH_LONG).show();
        }
    });
    dia.setNegativeButton("取消", new OnClickListener() {
                                                            
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(Info.this, "您点击了取消按钮", Toast.LENGTH_LONG).show();
                                                                
        }
    });
    dia.create();
    dia.show();
    //得到NotificationManager对象
    NotificationManager manager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    //取消通知,以免干扰用户
    manager.cancel(1);
} | 
    ◆结果

      干什么都不容易啊。。喝咖啡与喝茶水的区别是什么?
    
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。