Android 使用Notification
用惯了Android的人在刚拿到iPhone的时候,总是会习惯性的用手指从状态栏往下拖一下,这都是给Notification闹的。
不过Notification也确实是1个不错的提示工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。
今天我就以代码为主的形式来介绍Notification的使用,包括基本用法,自定义的View,以及更多的控制方法。
我们先看下Notification的几个主要组成部分:
Icon:不解释
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容
Notification的一般用法
取得NotificationManager
private NotificationManager mNotificationManager;mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Notification的滚动提示String tickerText = "My notification, It's a long text! Hello World desiyo?";//Notification的图标,一般不要用彩色的int icon = R.drawable.icon_02241_3; //contentTitle和contentText都是标准的Notification View的内容//Notification的内容标题,拖下来后看到的标题String contentTitle="My notification";//Notification的内容String contentText="Hello World!"; //Notification的Intent,即点击后转向的ActivityIntent notificationIntent = new Intent(this, this.getClass());notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //创建NotifcationNotification notification = new Notification(icon, tickerText, System.currentTimeMillis());//设定Notification出现时的声音,一般不建议自定义notification.defaults |= Notification.DEFAULT_SOUND;//设定如何振动notification.defaults |= Notification.DEFAULT_VIBRATE;//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身//这符合一般的Notification的运作规范notification.flags|=Notification.FLAG_AUTO_CANCEL;notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);//显示这个notificationmNotificationManager.notify(HELLO_ID, notification);
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="3dp"><ImageView android:id="@+id/notificationImage"android:layout_width="wrap_content" android:layout_height="wrap_content"android:src="@android:drawable/stat_sys_download"/><TextView android:id="@+id/notificationTitle"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_toRightOf="@id/notificationImage"android:layout_alignParentRight="true"android:paddingLeft="6dp"android:textColor="#FF000000"/><TextView android:id="@+id/notificationPercent"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_below="@id/notificationImage"android:paddingTop="2dp"android:textColor="#FF000000"/><ProgressBar android:id="@+id/notificationProgress"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_below="@id/notificationTitle"android:layout_alignLeft="@id/notificationTitle"android:layout_alignParentRight="true"android:layout_alignTop="@id/notificationPercent"android:paddingLeft="6dp"android:paddingRight="3dp"android:paddingTop="2dp"style="?android:attr/progressBarStyleHorizontal"/></RelativeLayout>
//Notification的滚动提示String tickerText1 = "Custom view for download notification";//Notification的图标,一般不要用彩色的int icon1 = android.R.drawable.stat_sys_download; //Notification的Intent,即点击后转向的ActivityIntent notificationIntent1 = new Intent(this, this.getClass());notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0); //创建NotifcationNotification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());//设定Notification出现时的声音,一般不建议自定义notification1.defaults |= Notification.DEFAULT_SOUND;//设定是否振动notification1.defaults |= Notification.DEFAULT_VIBRATE;//notification.number=numbers++;//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身//这符合一般的Notification的运作规范notification1.flags|=Notification.FLAG_ONGOING_EVENT; //创建RemoteViews用在Notification中RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android");contentView.setTextViewText(R.id.notificationPercent, "35%");contentView.setProgressBar(R.id.notificationProgress, 100, 35, false); notification1.contentView = contentView;notification1.contentIntent=contentIntent1; //显示这个notificationmNotificationManager.notify(CUSTOM_VIEW_ID, notification1);
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" /></animation-list>
//自定义提示音notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");//自定义振动方式long[] vibrate = {0,100,200,300};notification.vibrate = vibrate;
notification.number=notificationNumber;