首页 > 代码库 > 兔子--Notification的使用

兔子--Notification的使用

<span style="font-size:18px;color:#ff0000;"></span>
<span style="font-size:18px;color:#ff0000;">使用步骤:</span><p><span style="font-size:18px;color:#ff0000;">1 获取通知管理器NotificationManager,它也是一个系统服务</span></p><p><span style="font-size:18px;color:#ff0000;"></span></p><p><span style="font-size:18px;color:#ff0000;">2 建立通知Notification notification = new Notification(icon, null, when);</span></p><p><span style="font-size:18px;color:#ff0000;"></span></p><p><span style="font-size:18px;color:#ff0000;">3 为新通知设置参数(比如声音,震动,灯光闪烁)</span></p><p><span style="font-size:18px;color:#ff0000;"></span></p><p><span style="font-size:18px;color:#ff0000;">4 把新通知添加到通知管理器</span></p><div><span style="font-size:18px;color:#ff0000;"></span></div><span style="font-size:18px;color:#ff0000;"></span>
<span style="font-size:18px;color:#330000;"></span>
<span style="font-size:18px;color:#330000;"></span>
<span style="font-size:18px;color:#330000;"></span>
<span style="font-size:18px;color:#330000;">package com.example.mynotification;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;

import android.os.Bundle;

import android.text.NoCopySpan.Concrete;
import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

	Button m_Button1;

	TextView m_txtView;

	NotificationManager mNotificationManager;

	Notification mNotification;

	Intent mIntent;

	PendingIntent mPendingIntent;
	Context context;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		context = this;
		final Notification notification;
		mNotificationManager = (NotificationManager) this
				.getSystemService(NOTIFICATION_SERVICE);

		m_Button1 = (Button) this.findViewById(R.id.button1);

		// 点击通知时转移内容

		mIntent = new Intent(MainActivity.this, MainActivity1.class);

		mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
				mIntent, 0);

		notification = new Notification.Builder(context).setAutoCancel(true)
				.setContentTitle("qq正在运行").setContentText("qq,让交流更多方便")
				.setContentIntent(mPendingIntent)
				.setSmallIcon(R.drawable.ic_launcher)
				.setWhen(System.currentTimeMillis()).build();

		m_Button1.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {

				mNotificationManager.notify(0, notification);

			}
		});

	}
}
</span>






兔子--Notification的使用