Notification的应用
来源:互联网 发布:gta陈冠希捏脸数据 编辑:程序博客网 时间:2024/06/05 06:01
今天碰到Notification 捣鼓了好久,写个笔记免得以后又要用。
先传两张效果图
刚开始做的是老版本的不能像酷狗一样把通知栏内容缩小,后来换成4.1后支持的功能,可以缩放。
这是xml文件:
<?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" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="1dp" android:src="@drawable/backgro" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout></LinearLayout>三个全局变量()
private Notification notification;private NotificationManager mNotificationManager;private static final int CUSTOM_VIEW_ID = 1;在oncreate()方法中
//设置内容 RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification);contentView.setImageViewResource(R.id.imageview1, R.drawable.pic);contentView.setTextViewText(R.id.textview1, "通知內容 "); //使用Builder构造notification NotificationCompat.Builder builder = new NotificationCompat.Builder( this).setContent(contentView).setContentTitle("通知栏") .setContentText("点我进入").setSmallIcon(R.drawable.ic_launcher); notification = builder.build(); //判断当前版本,只有16开始才可以用缩放 if (Build.VERSION.SDK_INT >= 16) { notification.bigContentView = bigContentView;//使用bigContentView进行缩放 } else { notification.contentView = contentView; } //发送通知 Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity( MainActivity.this, CUSTOM_VIEW_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); notification.contentIntent = contentIntent; String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
在发送通知之前还可以做一些其他的处理比如
// 自定义提示音// SD卡内资源 String pathString = "/storage/sdcard1/1317.mp3"; File file = new File(pathString); notification.sound = Uri.fromFile(file);// raw内资源notification.sound = Uri.parse("android.resource://" + getPackageName()+ "/" + R.raw.mm); // 使用系统通知音 // notification.defaults=Notification.DEFAULT_SOUND;
//LED提示灯 notification.flags |= Notification.FLAG_SHOW_LIGHTS;notification.ledARGB = Color.BLUE;//根据设备不同可执行的颜色也不同notification.ledOnMS = 2000;//开启时间notification.ledOffMS = 2000;//关闭时间notification.defaults |= Notification.DEFAULT_LIGHTS;//震动 notification.defaults |= Notification.DEFAULT_VIBRATE; // 震动
// 声音一直响到用户相应,就是通知会一直响起,直到你触碰通知栏的时间就会停止(包括震动、提示音、LED闪烁) // notification.flags |= Notification.FLAG_INSISTENT;
最后再加入两个权限
LED闪烁<uses-permission android:name="android.permission.FLASHLIGHT" />
震动 <uses-permission android:name="android.permission.VIBRATE" />
0 0
- Notification的应用
- Notification的特殊应用
- Notification的特殊应用
- Notification的特殊应用
- Notification 的简易应用
- Notification的应用
- BroadCast 以及 Notification的应用
- Android Notification的简单应用
- Notification应用
- Android[初级教程]第十二章 Notification的应用
- android notification自定义布局的应用
- 【Notification】屏蔽特定应用的通知提示
- Notification中PendingIntent.Flag的应用
- Android Notification的基本应用 (8.1)
- Notification的一个简单应用代码示例
- 【Notification】屏蔽特定应用的通知提示
- Android Notification中PendingIntent.Flag的应用
- Android的Notification应用详解和PendingIntent的初步剖解
- 方法重写
- (五)四轴——卡尔曼滤波KalmanFilter
- spring bean scope 的几种类型
- mysql中的视图
- Activity的生命周期及加载模式
- Notification的应用
- n对括号有多少种匹配排列方式
- 黑马程序员_Java(StringBuffer,Arrays高级类,integer类,character类)
- Java实现通用组合算法
- GemFI的m5term连接端口退出问题
- 猫猫学iOS(四十七)之网易彩票帮助界面UIWebView的运用
- 机械传动机构
- wget和curl设置代理服务器的命令
- abstract(抽象) 的应用场景和需要注意的细节