`
kakukemeit
  • 浏览: 33432 次
  • 性别: Icon_minigender_2
  • 来自: 南京
社区版块
存档分类
最新评论

Android ApiDemos示例解析(26):App->Notification->IncomingMessage

 
阅读更多


 Android ApiDemos示例解析(26):App->Notification->IncomingMessage

http://blog.csdn.net/mapdigit/article/details/7669940

也可以参考李刚的《Android疯狂讲义》

 

应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信)。类NotificationManager 用来处理Notification, NotificationManager可以:
• 在Status Bar上显示一个新的图标。
• 在Extended status bar 窗口上显示附加信息或是启动一个Activity。
• 显示背光/LED。
• 使设备震动。
• 发出声音等。


对于一些没有UI的应用程序组件(如Broadcast Receiver, Services)或是非活动状态的Activity,Notification是推荐使用的可以提醒用户注意的方法。
Notification通常是在Status Bar上显示图标或是文字,此时用户如果想了解Notification的详细内容,可以按住Status Bar下拉显示Expanded Status bar 窗口,在Expanded Status bar窗口显示该Notification详情并可以启动对应的Activity。


IncomingMessage 示例介绍了Notification的一般用法:
1. 首先是取得NotificationManager 对象:

 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

2. 然后创建Notification,创建Notification时指定显示在Status bar的图标,文字以及显示Notification的时间:
 
Notification notif = new Notification(R.drawable.stat_sample, tickerText, System.currentTimeMillis());

 

3. 然后定义当用户打开Extented status windows窗口时的标题及详情。Notification常常代表了一个请求或者需要引起注意的事件,因此可以指定一个PendingIntent来响应用户点击这个Notification。

 // The details of our fake message  
CharSequence from = "Joe"; 
CharSequence message = "kthx. meet u for dinner. cul8r"; 

// The PendingIntent to launch our activity if the user selects this notification 

//指定一个PendingIntent来响应用户点击这个Notification。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, IncomingMessageView.class), 0);


// Set the info for the views that show in the notification panel. 
notif.setLatestEventInfo(this, from, message, contentIntent);


// after a 100ms delay, vibrate for 250ms, pause for 100 ms and 
// then vibrate for 500ms. 
notif.vibrate = new long[] { 100, 250, 100, 500};

 

4. 最后是触发这个Notification

 nm.notify(R.string.imcoming_message_ticker_text, notif);

一般来说对应同一个事件可以使用同一个Notification来通知用户,nm.notify的第一个参数为Notification 的ID,类型为整数。 可以使用同一个ID来表示同一个Notification,也可以使用这个ID来取消这个Notification,在IncomingMessage 中当用户点击显示了这个IncomingMessage详情后,会取消这个Notification(类IncomingMessageView中)。

 nm.cancel(R.string.imcoming_message_ticker_text);

 

 

public class IncomingMessage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.incoming_message);

		Button button = (Button) findViewById(R.id.notify);
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				showToast();
				showNotification();
			}
		});
	}

	/**
	 * The toast pops up a quick message to the user showing what could be the
	 * text of an incoming message. It uses a custom view to do so.
	 * 
	 * 自定义Toast的显示View
	 */
	protected void showToast() {
		// create the view
		View view = inflateView(R.layout.incoming_message_panel);

		// set the text in the view
		TextView tv = (TextView) view.findViewById(R.id.message);
		tv.setText("khtx. meet u for dinner. cul8r");

		// show the toast
		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
	}

	private View inflateView(int resource) {
		LayoutInflater vi = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		return vi.inflate(resource, null);
	}

	/**
	 * The notification is the icon and associated expanded entry in the status
	 * bar.
	 */
	protected void showNotification() {
		// look up the notification manager service
		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		// The details of our fake message
		CharSequence from = "Joe";
		CharSequence message = "kthx. meet u for dinner. cul8r";

		// The PendingIntent to launch our activity if the user selects this
		// notification
		// 指定一个PendingIntent来响应用户点击这个Notification。
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, IncomingMessageView.class), 0);

		// The ticker text, this uses a formatted string so our message could be
		// localized
		String tickerText = getString(R.string.imcoming_message_ticker_text,
				message);

		// construct the Notification object.
		Notification notif = new Notification(R.drawable.stat_sample,
				tickerText, System.currentTimeMillis());

		// Set the info for the views that show in the notification panel.
		notif.setLatestEventInfo(this, from, message, contentIntent);

		// after a 100ms delay, vibrate for 250ms, pause for 100 ms and
		// then vibrate for 500ms.
		notif.vibrate = new long[] { 100, 250, 100, 500 };

		// Note that we use R.layout.incoming_message_panel as the ID for
		// the notification. It could be any integer you want, but we use
		// the convention of using a resource id for a string related to
		// the notification. It will always be a unique number within your
		// application.
		nm.notify(R.string.imcoming_message_ticker_text, notif);
	}
}

 

 



 

源码工程:

  • 大小: 85.7 KB
分享到:
评论

相关推荐

    android API-DEMOS中文解析文档

    #### 2.26 App->Notification->IncomingMessage 介绍了如何实现消息通知,包括如何监听消息接收事件。 #### 2.27 App->Notification->NotifyingServiceController 讲解了如何利用服务来发送通知。 #### 2.28 App->...

    Android6.0 Api Demos

    Android 6.0 API Demos 是一个官方提供的示例代码集合,它展示了Android 6.0 (Marshmallow) SDK中的各种API功能和用法。这些示例旨在帮助开发者更好地理解和学习如何在实际应用中使用Android的新特性和API。下面将...

    Android ApiDemos

    `Android ApiDemos` 是一个官方提供的示例代码库,用于展示Android SDK中的各种API功能和用法。这个项目在Android 4.4.2(API级别19)中得到了详细体现,是开发者学习和理解Android系统API的重要资源。 **一、...

    Android4.1ApiDemos最新

    《深入解析Android 4.1 ApiDemos》 在Android开发领域,ApiDemos是一个非常重要的参考资料,它是由Google官方提供的示例代码集合,用于演示Android SDK中的各种API功能。对于开发者来说,尤其是对Android 4.1...

    ApiDemos 6.0源码

    《ApiDemos 6.0源码解析与学习指南》 ApiDemos是谷歌官方提供的一款用于展示Android SDK中各种API功能的示例应用。它包含了大量的代码示例,覆盖了从基本UI组件到高级系统服务的各种功能,是开发者深入理解和学习...

    android 4.0 api project

    这个ApiDemos项目是一个示例代码集合,旨在帮助开发者更好地理解和利用Android 4.0 API中的各种功能。以下是一些关键知识点: 1. **API级别**:Android 4.0对应的API级别是14。每个API级别都代表了Android系统的一...

    android apidome

    Android ApiDemos是一个重要的学习和开发资源,它包含了Android SDK中的各种API示例代码。这个压缩包文件很可能是Android Studio项目的形式,其中包含了用于演示Android API功能的各种代码片段和活动。ApiDemos通常...

Global site tag (gtag.js) - Google Analytics