`
Bauble
  • 浏览: 65862 次
  • 性别: Icon_minigender_1
  • 来自: Mercury
社区版块
存档分类
最新评论

Android32_Notification用法

阅读更多

 

Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。

实例:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/shownotification"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:text="显示通知"
/>
<Button android:id="@+id/deletenotification"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:text="清除通知"
/>
</LinearLayout>

 NotificationActivity.java

package com.android.activity;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationActivity extends Activity {
	private Button showNotification = null;
	private Button deleteNotification = null;
	//Notification管理器
	private NotificationManager nm = null;
	private static final int NOTIFICATION_ID = 123456;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        showNotification = (Button)findViewById(R.id.shownotification);
        deleteNotification = (Button)findViewById(R.id.deletenotification);
        showNotification.setOnClickListener(new ShowNotificationListener());
        deleteNotification.setOnClickListener(new DeletedNotificationListener());
    }
	class ShowNotificationListener implements OnClickListener{
		public void onClick(View v) {
		 showNotification(R.drawable.image,"图标边的文字","标题","内容");
		}
	}
	class DeletedNotificationListener implements OnClickListener{
		public void onClick(View v) {
			//表示当用户点击 Clear 之后,能够清除该通知。
			nm.cancel(NOTIFICATION_ID);
		}
	}
	public void showNotification(int icon,String tickertext,
			String title,String content){
		//创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。
		Notification notification = new Notification(icon,tickertext,System.currentTimeMillis());
		//这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
    	//振动为Notification.DEFAULT_VIBRATE;
    	//Light为Notification.DEFAULT_LIGHTS
    	//全部为Notification.DEFAULT_ALL
    	//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
    	notification.defaults = Notification.DEFAULT_ALL; 
    	//创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
    	//请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
    	//Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
    	//Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity
    	Intent intent = new Intent(this,NotificationActivity.class);
    	//PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位
    	PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0);
    	notification.setLatestEventInfo(this,title,content,pt);
    	//启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
    	nm.notify(NOTIFICATION_ID, notification);
    }
}

 运行结果:


 文字消失后:


 清除通知后:

  • 大小: 34.7 KB
  • 大小: 34.9 KB
  • 大小: 30.2 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics