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

Android 状态栏通知 Notification

2019年11月06日  | 移动技术网IT编程  | 我要评论

r906,煤炭粉,龙珠z改全集

    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);
            }
        });
    }

添加用户权限

试验效果

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网