加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android 状态栏通知 Notification

发布时间:2020-12-15 07:17:15 所属栏目:Java 来源:网络整理
导读:private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findVi
    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  onClick(View v) {
                 TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.);

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

添加用户权限

试验效果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读