android api分析34 Notification

本文介绍如何在Android应用中创建和管理通知,包括设置图标、文本内容、发送时间和声音等,并展示了如何使用PendingIntent实现通知点击后的页面跳转。

消息栏





例子效果图



public class MainActivity extends Activity
{
	static final int NOTIFICATION_ID = 0x1123;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//获取应用界面中的Button对象
		Button bn = (Button) findViewById(R.id.bn);
		//为按钮的单击事件绑定事件监听器
		bn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View source)
			{
				//创建一个启动其他Activity的Intent
				Intent intent = new Intent(MainActivity.this
					, OtherActivity.class);
				PendingIntent pi = PendingIntent.getActivity(MainActivity.this
					, 0, intent , 0);
				//创建一个Notification
				Notification notify = new Notification();
				//为Notification设置图标,该图标显示在状态栏
				notify.icon = R.drawable.ic_launcher;
				//为Notification设置文本内容,该文本会显示在状态栏
				notify.tickerText = "启动其他Activity的通知";
				//为Notification设置发送时间
				notify.when = System.currentTimeMillis();
				//为Notification设置声音
				notify.defaults = Notification.DEFAULT_SOUND;
				//为Notification设置默认声音、默认振动、默认闪光灯
				notify.defaults = Notification.DEFAULT_ALL;
				//设置事件信息
				notify.setLatestEventInfo(MainActivity.this, "普通通知",
					"点击查看", pi);
				//获取系统的NotificationManager服务
				NotificationManager notificationManager = (NotificationManager) 
					getSystemService(NOTIFICATION_SERVICE);
				//发送通知
				notificationManager.notify(NOTIFICATION_ID, notify);
			}
		});
		Button del = (Button)findViewById(R.id.del);
		del.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//获取系统的NotificationManager服务
				NotificationManager notificationManager = (NotificationManager) 
					getSystemService(NOTIFICATION_SERVICE);
				//取消通知
				notificationManager.cancel(NOTIFICATION_ID);
			}
		});
	}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加Notification" />

    <Button
        android:id="@+id/del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除Notification" />

</LinearLayout>
public class OtherActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//设置该Activity显示的页面
		setContentView(R.layout.other);	
	}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <!-- 定义一个ImageView -->

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
<!-- 添加操作闪光灯的权限 -->
		<uses-permission android:name="android.permission.FLASHLIGHT"/>
		<!-- 添加操作振动器的权限 -->
		<uses-permission android:name="android.permission.VIBRATE"/>
<activity android:name=".OtherActivity" android:label="other_activity">
            </activity>

notify.icon = R.drawable.ic_launcher;  设置图标,该图标显示在状态栏  
notify.tickerText = "启动其他Activity的通知";  设置文本内容,该文本会显示在状态栏  
notify.when = System.currentTimeMillis();  设置发送时间  
notify.defaults = Notification.DEFAULT_ALL;  设置默认声音、默认振动、默认闪光灯
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
This method was deprecated in API level 11. Use Notification.Builder instead.
notify.setLatestEventInfo(MainActivity.this, "普通通知",  "点击查看", pi);  
PendingIntent pi = PendingIntent.getActivity(MainActivity.this  , 0, intent , 0); 
PendingIntent
这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。可以理解为延迟执行的intent 


PendingIntent是对Intent一个包装。
 getActivity (Context context, int requestCode, Intent intent, int flags, Bundle options)


NotificationManager notificationManager = (NotificationManager)   
                    getSystemService(NOTIFICATION_SERVICE);  
notificationManager.notify(NOTIFICATION_ID, notify); 发送通知
notificationManager.cancel(NOTIFICATION_ID);  取消通知 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值