欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 综合教程 >内容正文

综合教程

Android 之 信息通知栏消息Notification

发布时间:2024/5/24 综合教程 48 生活家
生活随笔 收集整理的这篇文章主要介绍了 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>

总结

以上是生活随笔为你收集整理的Android 之 信息通知栏消息Notification的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。