通知 Notification

Notification,通知栏,在手机的最上面一行。在上面可以设置图片,文字。可以伴随出现声音,震动。下拉出现通知的菜单,有通知的标题,内容提示,等。点击出现通知要去的页面。

import android.app.Notification;

import android.app.NotificationManager;

一,获取通知资源

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

二,实例化一个通知
Notification notification = new Notification(icon, tickerText, when);

参数来源:

   通知栏上的图片icon: int icon = R.drawable.icon;       

   通知栏上的文字tickText: CharSquence tickerText = “hello”;      

   显示的时间when:  long when = System.currentTimeMillis();   

三,设置通知菜单上的内容和通知要去的页面。

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

参数来源:

the context for you Activity/Application: Context context = getApplicationContext();
通知标题:CharSequence contentTitle = "My notification";
通知内容提示:CharSequence contentText = "Hello World!";
要去的页面:Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

四,发出通知。

mNotificationManager.notify(HELLO_ID, notification);

参数来源:

通知Id: int HELLO_ID = 1; 也可设置为某个xml的id;int HELLO_ID = R.layout.id.hello;

通知:Notification notification参见步骤二。

另,清除通知: mNotificationManager.cancel(HELLO_ID);

拓展:

设置通知的声音,震动,闪光:

notification.defaults |= Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

震动:

设置权限<uses-permission android:name="android.permission.VIBRATE" />

notification.defaults |= Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

光:

notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

posted @ 2011-07-21 11:25  eoollo_fei  阅读(233)  评论(0)    收藏  举报