Notification用法复习(3.0以后版本)

使用Notification的示例代码:
public class Main extends Activity implements OnClickListener {

	private NotificationManager notificationManager;
	private Notification.Builder mBuilder;
	private Notification mNotification;

	private void showNotification(String tickerText, String contentTitle,
			String contentText, int smallIconId, int bigIconId) {
		// 如果同类型消息还显示在通知栏,则仅更新消息条数.
		if (mBuilder != null && mNotification != null) {
			RemoteViews contentView = mNotification.contentView;
			contentView.setTextViewText(R.id.fileName, contentText);
			mBuilder.setContentTitle(contentTitle);
			mBuilder.setTicker(tickerText);
			mBuilder.setWhen(System.currentTimeMillis());
			// mBuilder.setContentInfo("提示消息");
			// mBuilder.setContentText(contentText);
			notificationManager.notify(1, mBuilder.getNotification());
			return;
		}

		Intent notificationIntent = new Intent(this, Main.class);
		// 如果当前Activity启动在前台,则不开启新的Activity。
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		mBuilder = new Notification.Builder(this);

		// 设置下拉列表里的标题
		mBuilder.setContentTitle(contentTitle);

		// mBuilder.setContentInfo("提示消息");
		// mBuilder.setContentText(contentText);

		// 自定义显示消息内容
		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.inbox);
		contentView.setTextViewText(R.id.fileName, contentText);
		// 指定个性化视图
		mBuilder.setContent(contentView);

		mBuilder.setContentIntent(contentIntent);

		// mBuilder.setDeleteIntent(contentIntent);

		// 设置状态栏里面的图标(小图标)  
		mBuilder.setSmallIcon(smallIconId);

		// 下拉下拉列表里面的图标(大图标)
		mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
				R.drawable.smile));

		// 状态栏显示的标题文本.
		mBuilder.setTicker(tickerText);

		mBuilder.setDefaults(Notification.DEFAULT_SOUND);
		// 设置事件发生时间
		mBuilder.setWhen(System.currentTimeMillis());
		// Setting this flag will make it so the notification is automatically
		// canceled when the user clicks it in the panel.
		mBuilder.setAutoCancel(true);

		mNotification = mBuilder.getNotification();
		notificationManager.notify(1, mNotification);
	}

	int i = 0;
	int j = 0;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSmile:
			showNotification("您收到的消息数:" + (++i) + "条!", "消息数", "您收到的消息数:"
					+ (++j) + "条!", R.drawable.why, R.drawable.smile);
			break;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 实例化NotificationManager.
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Button btnSmile = (Button) findViewById(R.id.btnSmile);
		btnSmile.setOnClickListener(this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		System.out.println("onNewIntent() method called!!!");
		i = 0;
		j = 0;
	}
}


有一个点我们需要注意就是如果要在点击Notification栏的消息,打开本来已经启动的Activity时,我们需要在AndroidMenifest.xml配置文件中设置Activity的启动方式:
<activity
            android:name=".Main"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

猜你喜欢

转载自edison-cool911.iteye.com/blog/1715231