当前位置: 代码迷 >> Android >> Android学习札记- Notifying the User
  详细解决方案

Android学习札记- Notifying the User

热度:34   发布时间:2016-05-01 15:42:57.0
Android学习笔记-- Notifying the User

1.Toast 式的Notification


????? Toast Notification 是一种弹出式提示,它仅仅是自动显示一个消息,并自动消失,它不能接收任何的事件,不可以对其进行中继处理。Toast 可以在Activity 或者 Service中创建。如果在一个Service中创建一个Toast,它会在当前活动中的Activity显示。只有在自定义Toast的情况下才能调用Toast的构造函数来新建一个提示窗口,不是自定义时要用Toast.makeText(context, text, duration)获取一个Toast实例。

?

//默认的Toast提示		toastNotificationBtn.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				Context context = getApplicationContext();				CharSequence text = "Hello toast";				int duration = Toast.LENGTH_SHORT;				Toast toast = Toast.makeText(context, text, duration);				// 设置相对位置				toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);				toast.show();			}		});//自定义的Toast提示		Button customToastNotificationBtn = (Button) findViewById(R.id.customToastNotificationBtn);		customToastNotificationBtn.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				LayoutInflater inflater = getLayoutInflater();				View layout = inflater						.inflate(								R.layout.customer_toast_layout,								(ViewGroup) findViewById(R.layout.customer_toast_layout));				ImageView image = (ImageView) layout.findViewById(R.id.image);				image.setImageResource(R.drawable.icon);				TextView text = (TextView) layout.findViewById(R.id.text);				text.setText("Hello! This is a custom toast!");				Toast toast = new Toast(getApplicationContext());				toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);				toast.setDuration(Toast.LENGTH_LONG);				toast.setView(layout);				toast.show();			}		});

?

2.Status bar 中显示的 Notification如下:


?必要的配置:
? 1)要有一个图标资源
? 2)要有一个标题和展开消息时的消息内容
? 3)PendingIntent, 在点击消息内容时,就会激活PendingIntent。
?可选的配置:
? 1)在status bar中显示的内容
? 2)可播放一段音频文件
? 3)振动设置
? 4)LED灯设置

?

实现步骤:

?

1)取得NotificationManager的实例

String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

?

?2)初始化Notification

?

int icon = R.drawable.notification_icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);
?

? 3)定义展开时的内容和Intent

?

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);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
?

4)把消息显示到Status Bar上面

private static final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification);
?

最终例子如下:

?

//默认的提示barNotification.setOnClickListener(new OnClickListener(){			@Override			public void onClick(View v) {				int icon = R.drawable.icon;//icon from resources				CharSequence tickerText = "Hello";//ticker-text				long when = System.currentTimeMillis();//notification time				Context context = getApplicationContext();//application context				CharSequence contentTitle = "My notification";//expanded message title				CharSequence contentText = "Hello World!";//expanded message text								Intent notificationIntent = new Intent(getApplicationContext(),BarNotificationActivity.class);				PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);				Notification notification = new Notification(icon, tickerText, when);				notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);								NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);				notificationManager.notify(HELLO_ID, notification);			}				});//自定义的提示barNotificationCustom.setOnClickListener(new OnClickListener(){			@Override			public void onClick(View v) {				RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.customer_toast_layout);				contentView.setImageViewResource(R.id.image, R.drawable.icon);				contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");				Notification notification = new Notification(R.drawable.icon,"Hello",System.currentTimeMillis());				notification.contentView = contentView;								Intent notificationIntent = new Intent(getApplicationContext(),BarNotificationActivity.class);				PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);				notification.contentIntent = contentIntent;				NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);				manager.notify(CUSTOM_VIEW_ID, notification);			}		});
?

3. 更新Status bar的提示

? ? ? ? ? 可以更新已经存在的notification用来显示最新的内容,这样就比创建一个新的notification实例更好。

????????? 由于每个Notifycation都有一个标识号,我们可以通过NotificationManager来获取到它,并调用setLatestEventInfo()来更新提示的内容,然后调用notify()来触发它。但是对于自定义的 expanded view,用这些方法来更新提示是不起作用的。

?

5.给提示添加声音

?

notification.defaults |= Notification.DEFAULT_SOUND;notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
?

6.给提示添加振动效果

?

notification.defaults |= Notification.DEFAULT_VIBRATE;long[] vibrate = {0,100,200,300};notification.vibrate = vibrate;

?7.添加闪灯效果

?

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

? 8.其他效果

??? 通过给Notification添加fields和flags,还可以实现更多的效果。这些属性如下:

?"FLAG_AUTO_CANCEL" flag , 这个flags field将使Notification在用户选择了该提示之后自动消失。

?"FLAG_INSISTENT" flag , 不继重复播放音频直到用户响应。

?"FLAG_ONGOING_EVENT" flag

Add this to the flags field to group the notification under the "Ongoing" title in the Notifications window. This indicates that the application is on-going — its processes is still running in the background, even when the application is not visible (such as with music or a phone call).
"FLAG_NO_CLEAR" flag
Add this to the flags field to indicate that the notification should not be cleared by the "Clear notifications" button. This is particularly useful if your notification is on-going.
number field
This value indicates the current number of events represented by the notification. The appropriate number is overlaid on top of the status bar icon. If you intend to use this field, then you must start with "1" when the Notification is first created. (If you change the value from zero to anything greater during an update, the number is not shown.)
iconLevel field
This value indicates the current level of a LevelListDrawable that is used for the notification icon. You can animate the icon in the status bar by changing this value to correlate with the drawable's defined in a LevelListDrawable. See the LevelListDrawable reference for more information.

?

  相关解决方案