Android Notification(一)使用通知
来源:互联网 发布:opengl 纹理变形算法 编辑:程序博客网 时间:2024/04/30 22:56
Android创建通知一般有以下几步:
1.获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2.创建点击通知要启动的Activity
PendingIntent intent = PendingIntent.getActivity( MainActivity.this, 0, new Intent(MainActivity.this, NotificationActivity.class), 0);
3.创建通知
Notification notification = new Notification.Builder( MainActivity.this).setSmallIcon(R.drawable.ic_launcher) .setTicker("新消息!!!").setContentTitle("你好") .setContentText("交个朋友把").setContentIntent(intent) .setNumber(mNumber++).build();
4.配置通知选项
notification.defaults = Notification.DEFAULT_SOUND;notification.ledARGB = Color.GREEN;notification.ledOnMS = 1000;notification.ledOffMS = 1000;long[] vibrates = { 0, 1000, 1000, 1000 };notification.vibrate = vibrates;notification.flags = Notification.FLAG_SHOW_LIGHTS;
5.管理器发送通知
manager.notify(0x1111, notification);
这里说明下:
1.manager 是获取系统的通知管理器。在默认情况下,会有一些通知固定在通知管理器中,像QQ、360都有。使用通知管理器就可以管理我们自己的通知。
2.intent 中MainActivity是在该Activity中发送通知,NotificationActivity是点击通知栏的通知进入的Activity。
3.一些基础的配置
4.ledARGB 是配置手机的闪光灯,vibrate是配置手机震动,需要添加权限。
<uses-permission android:name="android.permission.VIBRATE" />
notification.defaults = Notification.DEFAULT_SOUND; 是配置提示音为系统默认提示音。
5.管理器发送通知,第一个参数是通知的序号,如果通知栏有了这个序号的通知,再次发送通知就不会重新创建一个通知栏信息。
动手测试。
创建两个Activity:
public class MainActivity extends Activity { private NotificationManager manager; private int mNumber = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); }}
public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); }}
修改对于的布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送通知" /> <Button android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消通知" /></LinearLayout>
notification_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="This is notification layout" android:textSize="24sp" /></RelativeLayout>
添加对于按钮的事件处理
Button send = (Button) findViewById(R.id.send);send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub PendingIntent intent = PendingIntent.getActivity( MainActivity.this, 0, new Intent(MainActivity.this, NotificationActivity.class), 0); Notification notification = new Notification.Builder( MainActivity.this).setSmallIcon(R.drawable.ic_launcher) .setTicker("新消息!!!").setContentTitle("你好") .setContentText("交个朋友把").setContentIntent(intent) .setNumber(mNumber++).build(); notification.defaults = Notification.DEFAULT_SOUND; notification.ledARGB = Color.GREEN; notification.ledOnMS = 1000; notification.ledOffMS = 1000; long[] vibrates = { 0, 1000, 1000, 1000 }; notification.vibrate = vibrates; notification.flags = Notification.FLAG_SHOW_LIGHTS; manager.notify(0x1111, notification); }});Button cancel = (Button) findViewById(R.id.cancel);cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { manager.cancel(0x1111); }});
manager.cancel(0x1111); 可以取消这个通知
这个程序要用到闪光灯和震动效果,一般最后是在真机上测试。编译生成apk,放到手机上,测试就可以看出效果了。
0 0
- Android Notification(一)使用通知
- Android通知Notification的使用(一)
- Android 使用Notification(通知)
- android Notification通知使用
- android通知(Notification)的使用
- android Notification(通知) 的使用
- Android 通知 Notification 使用详情
- Android通知Notification的使用
- android 使用通知(Notification)
- Android中使用通知Notification
- Android 使用通知(Notification)
- Android通知(Notification)使用详解
- Android Notification 通知 简单使用
- android Notification 通知栏使用
- ANDROID NOTIFICATION 通知的使用
- Android中使用Notification实现普通通知栏(Notification示例一)
- Android通知Notification入门小例子(一)
- Android中通知的使用-----Notification详解
- C#中关于DataGridView行和列的背景色-前景色设置
- 详解build.gradle
- iOS_APP打包上架流程
- DirectFB的接口详解
- 龙芯麒麟操作系统安装 PostgreSql
- Android Notification(一)使用通知
- myeclipse 反向工程映射持久化类注意事项
- Fragment嵌套问题,Fragment后台操作界面replace
- GitHub TOP 100Android开源库
- 40个Java多线程问题总结
- 组播IP地址与组播MAC地址之间的换算方法与例子
- 关于android控件drawableLeft图片大小控制
- Android 文字倾斜
- 多线程、特别是NSOperation 和 GCD 的内部原理