欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 状态栏通知 Notification

程序员文章站 2022-07-04 22:31:56
private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCrea... ......
    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);
            }
        });
    }

添加用户权限

试验效果