zoukankan      html  css  js  c++  java
  • Android 状态栏通知 Notification

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

    添加用户权限

    image

    试验效果

    image

    image

  • 相关阅读:
    mvc3在各个IIS版本中的部署
    linq学习
    常用的正则表达式
    Jenkins+Git+Maven+Tomcat的初步学习
    12个用得着的JQuery代码片段
    JQuery原理介绍及学习方法
    【前端学习】javascript面向对象编程(继承和复用)
    c# throw和throw ex
    .net 信息采集ajax数据
    C# FileSystemWatcher 并发
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11801278.html
Copyright © 2011-2022 走看看