android 弹出消息框
来源:互联网 发布:网络视频协议 编辑:程序博客网 时间:2024/05/18 03:53
那么这种效果是如何实现的呢,其实很简单,这用到了系统中的两个类:Notification和NotificationManager 一个简单例子:比如说闹钟
1、布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/send" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="发送通知"android:onClick="click"></Button><Button android:layout_width="fill_parent" android:id="@+id/cancel"android:layout_height="wrap_content" android:text="取消通知"android:onClick="click"></Button></LinearLayout>
2、代码
public class MianActivity extends Activity {NotificationManager nm = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void click(View v) {switch (v.getId()) {case R.id.send:String service = Context.NOTIFICATION_SERVICE; nm = (NotificationManager) getSystemService(service); //获得系统级服务,用于管理消息Notification n = new Notification(); //定义一个消息类n.icon = R.drawable.icon; //设置图标n.tickerText = "Notification Test!!"; // 设置消息n.when = System.currentTimeMillis(); //设置时间// Notification n1 =new Notification(icon,tickerText,when); //也可以用这个构造创建Intent intent = new Intent(MianActivity.this, MianActivity.class); PendingIntent pi = PendingIntent.getActivity(MianActivity.this, 0,intent, 0); //消息触发后调用n.setLatestEventInfo(MianActivity.this, "my title", "my content",pi); //设置事件信息就是拉下标题后显示的内容nm.notify(1, n); //发送通知break;case R.id.cancel:nm.cancel(1); //关闭通知}}}运行效果如下:正如上边截图那样,可以为Notification对象设置图标、显示文字等信息,除了这些还有很多属性可以用来提醒。如:声音、震动、闪光灯等。设置方式如下:1、播放音乐 n.defaults |= Notification.DEFAULT_SOUND; n.sound=Uri.parse("file:///sdcard/sound.mp3"); n.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");2、振动 n.defaults|=Notification.DEFAULT_VIBRATE; long[] vib={0,50,100,150,200}; n.vibrate=vib; 记得加权限:<uses-permission android:name="android.permission.VIBRATE"></uses-permission>3、闪光灯n.defaults|=Notification.DEFAULT_LIGHTS; n.ledARGB=0x99f0ff00;n.ledOnMS=300; n.ledOffMS=2000; n.flags|=Notification.FLAG_SHOW_LIGHTS;
阅读全文
0 0
- android弹出消息框
- android 弹出消息框
- 弹出消息框 javascript
- 弹出消息框
- 弹出消息框
- 弹出消息框
- QT弹出消息框
- div弹出消息框
- C++弹出消息框
- android弹出消息框、输入框、选择框、日期选择
- Android提供常用的消息弹出框,Toast和Alert。
- javascript弹出消息提示框
- 背景变暗弹出消息框
- lazycode 弹出消息框类
- 【ASP.NET】 弹出消息框
- Sharepoint 弹出消息提示框
- C#右下角弹出消息框
- jquery easyui 弹出消息框
- XML文档的语法规则(儿歌)
- POJ 3481 Double Queue(STL之双向优先队列)
- HashTable与HashMap的区别
- Day01 varnish
- CSS之解决高度塌陷问题方法终极版
- android 弹出消息框
- 《机器学习实战》学习笔记-[8]-回归-普通最小二乘OLS
- MYSQL学习笔记(九)分组数据
- JSP/Servlet
- tomcat安装-->使用homebrew安装tomcat并且运行tomcat服务器
- #bzoj2239#猜谜语(DP)
- UCML自定义查询(导致点击列头排序弹出异常)
- Python的深拷贝与浅拷贝
- ASP.net—总结