当前位置: 萬仟网 > 移动技术>移动开发>Android > Android 状态栏通知 Notification

Android 状态栏通知 Notification

2019年11月06日  | 萬仟网移动技术  | 我要评论
    private notificationmanager manager;
    private notification.builder builder;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        button = (button) findviewbyid(r.id.button);
        // 创建一个通知管理类
        manager = (notificationmanager) getsystemservice(notification_service);
        builder = new notification.builder(this);

        button.setonclicklistener(new onclicklistener() {

            @override
            public void onclick(view v) {
                // todo auto-generated method stub
                intent intent = new intent(mainactivity.this, mainactivity.class);
                pendingintent pendingintent = pendingintent.getactivity(mainactivity.this, 0, intent, 0);

                builder.setcontentintent(pendingintent);
                builder.setcontenttitle("new notification here");
                builder.setcontenttext("hello furong");
                builder.setsmallicon(r.drawable.a1);
                builder.setticker("有通知来了"); // 第一次出现在状态栏
                // 需要用户授权 android.permission.vibrate
                builder.setdefaults(notification.default_all); // 所有的提示都默认

                notification notification = builder.build();
                manager.notify(1000, notification);
            }
        });
    }

添加用户权限

试验效果

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
Copyright © 2017-2020  萬仟网 保留所有权利. 粤ICP备17035492号-1
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com