zoukankan      html  css  js  c++  java
  • Android 之 信息通知栏消息Notification

    Notification是安卓手机顶部的消息提示

    这里我们分别设置两个按钮,来实现顶部消息的发送和取消

    功能实现

    首先要在主Activity中设置一个通知控制类

    NotificationManager manager; //通知控制类
    

     然后在onCreate方法中获取系统服务

    manager  = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    

     接着设置通知栏信息

     private void sendNotification() {
            Intent intent = new Intent(this,MainActivity.class);
            PendingIntent pintent = PendingIntent.getActivity(this,0,intent,0);
            Builder builder = new Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
            builder.setTicker("hello");//设置手机状态栏提示
            builder.setWhen(System.currentTimeMillis());//时间
            builder.setContentTitle("通知通知栏");//标题
            builder.setContentText("我是小浩");//通知内容
            builder.setContentIntent(pintent);//点击后的意图
            builder.setDefaults(Notification.DEFAULT_ALL);//给通知设置震动,声音,和提示灯三种效果,不过要记得申请权限
            Notification notification = builder.build(); //4.1版本以上用这种方法
            //builder.getNotification();   //4.1版本以下用这种方法
            manager.notify(notification_ID,notification);
        }
    

     在Mainfest文件中加入 震动和提示灯权限

     <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
       <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
    
  • 相关阅读:
    Python基础-字符串方法 、文件操作
    Python基础-列表、字典
    Python基础作业-用户登录
    LeetCode 78. Subsets
    LeetCode 77. Combinations
    LeetCode 76. Minimum Window Substring
    LeetCode 74. Search a 2D Matrix
    LeetCode 73. Set Matrix Zeroes
    LightOJ 1043
    LightOJ 1042
  • 原文地址:https://www.cnblogs.com/tonghao/p/5723574.html
Copyright © 2011-2022 走看看