Android入门之Notification(纯java)
来源:互联网 发布:软件系统开发公司 编辑:程序博客网 时间:2024/05/18 01:18
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入门之Notification(纯java)
- Android入门之TextView(纯java)
- Android入门之EditText(纯java)
- Android入门之RadioGroup(纯java)
- Android入门之Spinner(纯java)
- Android入门之AutoCompleteTextView(纯java)
- Android入门之DatePicker(纯java)
- Android入门之TimePicker(纯java)
- Android入门之ScrollView(纯java)
- Android入门之ProgressBar(纯java)
- Android入门之SeekBar(纯java)
- Android入门之RatingBar(纯java)
- Android入门之ImageView(纯java)
- Android入门之ImageButton(纯java)
- Android入门之Gallery(纯java)
- Android入门之GridView(纯java)
- Android入门之Menu(纯java)
- Android入门之ContextMenu(纯java)
- [小说连载]张小庆,在路上(19)- 真的要在北京吗
- wait() notify()----最简单实例告诉你怎么用
- 12球称重问题思维分析(updated #1)
- 宏中"#"和"##"的用法
- 一道关于实例化顺序的C#面试题
- Android入门之Notification(纯java)
- Windows Phone 开发教学系列视频之四天玩转 Windows Phone 开发(附下载地址)
- 从app.config中读取连接字符串
- 大小写转换
- Android-Notification的使用
- 每个程序员都必须遵守的编程原则
- CentOS 4.8 安装snavigator 编译出错解决办法
- Intent和PendingIntent的区别
- NO.70 AR Tools-批量文本关键字替换工具