通知栏(Notification)的基本使用
来源:互联网 发布:360解压软件下载 编辑:程序博客网 时间:2024/05/19 16:50
通知是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台运行时,可以通过通知来实现。发送一个通知,手机的最上方会出现通知的图标,下拉状态栏的时候,可以看到通知的详细内容。
一.通知的基本使用:
(1).先获取通知的管理者对象,通过下面的方式获取
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
(2).开启通知前,需要一个Notification的对象,由于版本的兼容,采用V4包下的NotificationCompat类来获取
Notification notification = new NotificationCompat.Builder(this).build();
(3).发送一个通知(一个id对应一个通知)
manager.notify(1,notification);
二.构建一个丰富多样的通知显示代码:
布局代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_send" android:text="发送一条通知" android:textColor="#ff0000" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } }); } private void sendNotification() { //1.获取通知管理者对象 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //如果需要通知可点击,需要使用PendingIntent(延迟意图) Intent intent = new Intent(MainActivity.this,NotificationActivity.class); //参数:(上下文,请求码(通常填0,不常用),意图对象,行为(通常填0)) PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); //2.获取一个notification对象实例 Notification notification = new NotificationCompat.Builder(this) .setContentTitle("银行到账")//设置标题 .setContentText("工商银行为你转入10000000000,你卡上余额为10000003460。")//设置内容 .setWhen(System.currentTimeMillis())//设置通知的时间 .setSmallIcon(R.drawable.zhifu_icon_payply)//设置小图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.zhifu_icon_payply))//设置大图标 .setContentIntent(pendingIntent)//延迟处理的意图,当点击通知时 .setAutoCancel(true)//设置当用户点击面板时通知自动取消 //.setSound(Uri.fromFile(new File(".....")))//设置声音播放 //.setLights(Color.GREEN,1000,1000)//设置LED闪烁灯 .setDefaults(NotificationCompat.DEFAULT_ALL)//使用默认的通知选项 .setPriority(NotificationCompat.PRIORITY_MAX)//设置通知的优先级 .build(); //3.开启通知 manager.notify(1,notification); }}
阅读全文
0 0
- 通知栏(Notification)的基本使用
- Notification通知栏的基本使用
- 通知栏Notification基本使用
- 关于Notification通知的基本使用
- Android 通知栏 Notification 基本使用
- Notification通知栏的使用
- Notification通知栏的使用
- Notification 通知栏的使用
- NOtification(通知)的使用
- notification(通知)的使用
- notification(通知)的使用
- 安卓中通知(Notification)的基本使用方法
- 安卓中通知(Notification)的基本使用方法
- Notification通知的使用
- Notification通知的基本用法
- Notification--------通知的基本用法
- 通知栏Notification使用
- 使用通知(Notification)
- 篇二:二叉树的一生
- NOIP2017提高组 模拟赛16(总结)
- Maven的Helloworld
- 凯儿得乐企业化系统平台开发
- 相反数
- 通知栏(Notification)的基本使用
- 使用python实现二分查找
- day11-Tomcat与Servlet入门
- new和newInstance() Constructor.newInstance()区别
- 展讯7731C_M Android6.0 充电指示灯实现
- 常用数据库连接描述符
- Android瘦身之tiny图片处理
- 用python 实现链表(实现__getitem__,__set__,__len__ 魔法方法)
- Wannafly模拟赛 矩阵 [矩阵hash+二分]