

NotificationTest.java
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.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class NotificationTest extends Activity {
private LinearLayout mainView=null;
private Button button1=null;
private Button button2=null;
private Button button3=null;
private NotificationManager nm=null;
private Notification notification1=null;
private Notification notification2=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nm_init();
mainView=new LinearLayout(this);
mainView.setOrientation(LinearLayout.VERTICAL);
//
button1=new Button(this);
button1.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
button1.setText("模拟显示一个未接电话的通知");
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(notification1==null){
notification1_init();
}
nm.notify(1, notification1);
}
});
mainView.addView(button1);
//
button2=new Button(this);
button2.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
button2.setText("LED闪烁控制");
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(notification2==null){
notification2_init();
}
nm.notify(2, notification2);
nm.cancel(2);
}
});
mainView.addView(button2);
//
button3=new Button(this);
button3.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
button3.setText("结束通知");
button3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
try{
//结束通知
nm.cancel(1);
}catch(Exception ex){}
}
});
mainView.addView(button3);
setContentView(mainView);
}
/*nm初始化*/
void nm_init(){
nm=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
}
/*notification1初始化*/
void notification1_init(){
notification1=new Notification();
//设置图标
notification1.icon=android.R.drawable.sym_call_missed;//设置图标
//设置图标级别,如标题栏有多个图标,如qq、短信息等,图标级别可以作为排序依据,默认为0
notification1.iconLevel=0;
//设置提示信息
notification1.tickerText="这是一个提示信息";
//时间戳
notification1.when=System.currentTimeMillis();
//设置消息数,如未读短信数量,数量会在图标之前显示,默认为0,0和负数不显示
notification1.number=0;
//设置提示音、LED灯和振动效果,DEFAULT_ALL表示三个效果都默认
//DEFAULT_LIGHTS表示LED等的闪烁效果采用系统默认
//DEFAULT_SOUND表示声音提示采用系统默认
//DEFAULT_VIBRATE表示振动效果采用系统默认
//我么也可以通过参数来订制各种不同效果
//提示音参数audioStreamType、sound
//led参数ledARGB、ledOffMS、ledOnMS
//振动效果参数vibrate
//注意,振动要权限android.permission.VIBRATE
notification1.defaults=Notification.DEFAULT_ALL;
//其他属性
Context context=this;
String contentTitle="这是标题";
String contentText="这是内容";
//跳转页面
Intent notificationIntent = new Intent(NotificationTest.this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//
notification1.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
}
/*notification2初始化*/
void notification2_init(){
notification2=new Notification();
notification2.ledARGB=0x00ff00; //设置LED颜色为绿色,红色0xff0000
notification2.ledOnMS = 100;
notification2.ledOffMS = 100;
notification2.flags = Notification.FLAG_SHOW_LIGHTS;
}
}
MyClass.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyClass extends Activity{
private LinearLayout mainView=null;
private TextView tv=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView=new LinearLayout(this);
tv=new TextView(this);
tv.setText("这是一个简单的跳转页面,无任何参数跳转。实际应用中,一般会通过Intent来传递参数。");
tv.append("由于此处暂时没有使用参数传递,所以无法取消通知。");
mainView.addView(tv);
setContentView(mainView);
}
}
本文介绍了一个关于Android通知机制的应用示例,展示了如何创建不同类型的系统通知,包括模拟未接来电通知、LED闪烁控制及通知的取消。通过具体代码演示了通知的创建、配置和触发过程。
8480

被折叠的 条评论
为什么被折叠?



