安卓手机下拉状态栏的代码实现

1. 对于高版本的sdk, (16以上)

 1 //高版本的通知栏,最低要求sdk版本为16
 2         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 3         
 4         //链式编程,每次返回的都是一个builder对象
 5         Notification notification = new Notification.Builder(this)
 6         .setContentTitle("标题")
 7         .setContentText("内容")
 8         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
 9         .setSmallIcon(R.drawable.ic_launcher)
10         .build();
11         nm.notify(1, notification);

显示效果:

2. 对于低版本的sdk

 1         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 2         
 3         //第二个参数为:在状态栏上翻动显示的文本
 4         Notification notification = new Notification(R.drawable.ic_launcher, "出来了?", System.currentTimeMillis());
 5         
 6         //指定点击通知之后,跳转一个界面,以打电话为例
 7         Intent intent = new Intent();
 8         intent.setAction(Intent.ACTION_CALL);
 9         intent.setData(Uri.parse("tel://110"));
10         
11         // 延期的意图对象 ---用于描述 将来干什么事儿
12         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
13         
14         //设置拖动通知下来之后,展示的内容以及点击之后跳转到的界面
15         notification.setLatestEventInfo(this, "标题", "内容", contentIntent );
16         
17         nm.notify(1, notification);

显示效果:

这里笔者以点击后打电话为例

原文地址:https://www.cnblogs.com/wanghaoyuhappy/p/5294075.html