手机有未接电话和短信时,会在手机顶部状态栏出现一个提示图标,按住状态栏往下拖,就可以展开查看这些快讯信息!android平台提供了NotificationManager去管理状态栏信息,提供Notification处理这些快讯。
首先通过getSystemService方法得到NotificationManager对象,在对Notification的内容、图标、标题等属性进行设置后,就可以调用notify方法去执行一个Notification快讯。具体请看下面示例:
先看看运行效果,如图:
查看快讯用activity02显示
布局文件非常简单,就是4个Button,这里就省略了!
statusActivity.java
package com.status;
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;
import android.widget.TextView;
public class statusActivity extends Activity {
Button bt1,bt2,bt3,bt4;
//申明通知消息管理器
NotificationManager notificationManager;
Intent intent;
PendingIntent pIntent1,pIntent2,pIntent3,pIntent4;
//声明Notification对象
Notification notification;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化NotificationManager对象
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
bt3=(Button) findViewById(R.id.button3);
bt4=(Button) findViewById(R.id.button4);
intent=new Intent(statusActivity.this,activity02.class);
//显示点击通知时显示内容的类
pIntent1=PendingIntent.getActivity(statusActivity.this, 0, intent, 0);
pIntent2=PendingIntent.getActivity(statusActivity.this, 0, intent, 0);
pIntent3=PendingIntent.getActivity(statusActivity.this, 0, intent, 0);
pIntent4=PendingIntent.getActivity(statusActivity.this, 0, intent, 0);
notification=new Notification();
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notification.icon=R.drawable.aws;
notification.tickerText="第一个消息";
notification.defaults=Notification.DEFAULT_SOUND;通知时发出默认的声音
notification.setLatestEventInfo(statusActivity.this, "消息1", "我是第一个按钮", pIntent1);
notificationManager.notify(0,notification);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notification.icon=R.drawable.dsd;
notification.tickerText="第二个消息";
notification.defaults=Notification.DEFAULT_VIBRATE;//通知时震动
notification.setLatestEventInfo(statusActivity.this, "消息2", "我是第二个按钮", pIntent2);
notificationManager.notify(1,notification);
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notification.icon=R.drawable.fd;
notification.tickerText="第三个消息";
notification.defaults=Notification.DEFAULT_LIGHTS;//通知时屏幕发亮
notification.setLatestEventInfo(statusActivity.this, "消息3", "我是第三个按钮", pIntent3);
notificationManager.notify(2,notification);
}
});
bt4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notification.icon=R.drawable.ffdf;
notification.tickerText="第四个消息";
notification.defaults=Notification.DEFAULT_VIBRATE;//通知时震动
notification.setLatestEventInfo(statusActivity.this, "消息4", "我是第四个按钮", pIntent4);
notificationManager.notify(3,notification);
}
});
}
}
activity02.java
package com.status;
import android.app.Activity;
import android.os.Bundle;
public class activity02 extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
注意:应为使用了3个activity,所以要在AndroidManfest.xml文件中申明,否则会出错!