android自定义的notification

来源:互联网 发布:松竹歌舞伎 知乎 编辑:程序博客网 时间:2024/06/08 13:50


2014年4月26日 
消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示。本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo。

先来看看效果图:
附上源码:http://download.csdn.net/detail/wwj_748/7259815
有兴趣的朋友可以加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)


上面就是通知栏的效果了,我们主要改的地方有大头像,小头像,标题,内容等,直接看代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.wwj.custom.notification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.graphics.BitmapFactory;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. /** 
  15.  * 自定义Notification 
  16.  *  
  17.  * @author wwj 
  18.  * 
  19.  */  
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.   
  22.     private Button showNotification;  
  23.     private Button showCustomNotifi;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.   
  30.         showNotification = (Button) findViewById(R.id.button1);  
  31.         showCustomNotifi = (Button) findViewById(R.id.button2);  
  32.   
  33.         showNotification.setOnClickListener(this);  
  34.         showCustomNotifi.setOnClickListener(this);  
  35.   
  36.     }  
  37.   
  38.     @Override  
  39.     public void onClick(View v) {  
  40.         switch (v.getId()) {  
  41.         case R.id.button1:  
  42.             send();  
  43.             break;  
  44.         case R.id.button2:  
  45.             custom();  
  46.             break;  
  47.   
  48.         default:  
  49.             break;  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 旧方法 
  55.      */  
  56.     public void send() {  
  57.         // 1 得到通知管理器  
  58.         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  59.   
  60.         // 2构建通知  
  61.         Notification notification = new Notification(  
  62.                 android.R.drawable.stat_notify_chat, "这是提示信息",  
  63.                 System.currentTimeMillis());  
  64.   
  65.         // 3设置通知的点击事件  
  66.         Intent intent = new Intent(this, MainActivity.class);  
  67.         PendingIntent contentIntent = PendingIntent.getActivity(this100,  
  68.                 intent, 0);  
  69.         notification.setLatestEventInfo(this"通知的标题""通知的内容", contentIntent);  
  70.   
  71.         notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失  
  72.   
  73.         // 4发送通知  
  74.         nm.notify(100, notification);  
  75.     }  
  76.   
  77.     /** 
  78.      * 自定义Notification 新方法 
  79.      * 新的方法,本人在手机测试会崩溃,如果不行的话,可以继续使用旧的构建方法,毕竟高版本会兼容低版本的 
  80.      */  
  81.     public void custom() {  
  82.         // 1 得到通知管理器  
  83.         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  84.         // 2 设置通知的点击事件  
  85.         Intent intent = new Intent(this, MainActivity.class);  
  86.         PendingIntent contentIntent = PendingIntent.getActivity(this100,  
  87.                 intent, 0);  
  88.         // 3构建通知  
  89.         Notification.Builder builder = new Notification.Builder(this)  
  90.         // API 11添加的方法  
  91.                 .setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)  
  92.                 // 设置状态栏的小标题  
  93.                 .setLargeIcon(  
  94.                         BitmapFactory.decodeResource(getResources(),  
  95.                                 R.drawable.jay))// 设置下拉列表里的图标  
  96.                 .setWhen(System.currentTimeMillis()).setTicker("凤姐来啦")// 设置状态栏的显示的信息  
  97.                 .setAutoCancel(true)// 设置可以清除  
  98.                 .setContentTitle("通知通知"// 设置下拉列表里的标题  
  99.                 .setContentText("凤姐即将光临天拓游戏,各部门做好防雷准备"); // 设置可以清除  
  100.         Notification notification = builder.build();// API 16添加创建notification的方法  
  101.         // 通知  
  102.         manager.notify(110, notification);  
  103.   
  104.         // // 2构建通知  
  105.         // Notification notification2 = new Notification(R.drawable.jay, "天拓游戏",  
  106.         // System.currentTimeMillis());  
  107.         //  
  108.         // // 3设置通知的点击事件  
  109.         // Intent intent2 = new Intent(this, MainActivity.class);  
  110.         // PendingIntent contentIntent2 = PendingIntent.getActivity(this, 100,  
  111.         // intent2, 0);  
  112.         // notification2.setLatestEventInfo(this, "天拓游戏", "天拓游戏有个技术部",  
  113.         // contentIntent2);  
  114.         //  
  115.         // notification2.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失  
  116.         //  
  117.         // // 4发送通知  
  118.         // manager.notify(100, notification2);  
  119.     }  
  120. }  


稍微提一下的是,我们都知道Android SDK版本的变迁,API也会跟着遍,每个版本的API都可能会增删改一些接口,我们在使用Android为我们开发者提供的一些方法的时候,需要注意版本之间的区别,假如我们使用高版本的API的话,可能会引起一些错误,低版本的话可能在开发中又不适用了,反正程序不崩溃是最起码的保证,至于程序的功能能实现用什么API都是可以的。

2014年4月26日 
消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示。本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo。

先来看看效果图:
附上源码:http://download.csdn.net/detail/wwj_748/7259815
有兴趣的朋友可以加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)


上面就是通知栏的效果了,我们主要改的地方有大头像,小头像,标题,内容等,直接看代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.wwj.custom.notification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.graphics.BitmapFactory;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. /** 
  15.  * 自定义Notification 
  16.  *  
  17.  * @author wwj 
  18.  * 
  19.  */  
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.   
  22.     private Button showNotification;  
  23.     private Button showCustomNotifi;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.   
  30.         showNotification = (Button) findViewById(R.id.button1);  
  31.         showCustomNotifi = (Button) findViewById(R.id.button2);  
  32.   
  33.         showNotification.setOnClickListener(this);  
  34.         showCustomNotifi.setOnClickListener(this);  
  35.   
  36.     }  
  37.   
  38.     @Override  
  39.     public void onClick(View v) {  
  40.         switch (v.getId()) {  
  41.         case R.id.button1:  
  42.             send();  
  43.             break;  
  44.         case R.id.button2:  
  45.             custom();  
  46.             break;  
  47.   
  48.         default:  
  49.             break;  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 旧方法 
  55.      */  
  56.     public void send() {  
  57.         // 1 得到通知管理器  
  58.         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  59.   
  60.         // 2构建通知  
  61.         Notification notification = new Notification(  
  62.                 android.R.drawable.stat_notify_chat, "这是提示信息",  
  63.                 System.currentTimeMillis());  
  64.   
  65.         // 3设置通知的点击事件  
  66.         Intent intent = new Intent(this, MainActivity.class);  
  67.         PendingIntent contentIntent = PendingIntent.getActivity(this100,  
  68.                 intent, 0);  
  69.         notification.setLatestEventInfo(this"通知的标题""通知的内容", contentIntent);  
  70.   
  71.         notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失  
  72.   
  73.         // 4发送通知  
  74.         nm.notify(100, notification);  
  75.     }  
  76.   
  77.     /** 
  78.      * 自定义Notification 新方法 
  79.      * 新的方法,本人在手机测试会崩溃,如果不行的话,可以继续使用旧的构建方法,毕竟高版本会兼容低版本的 
  80.      */  
  81.     public void custom() {  
  82.         // 1 得到通知管理器  
  83.         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  84.         // 2 设置通知的点击事件  
  85.         Intent intent = new Intent(this, MainActivity.class);  
  86.         PendingIntent contentIntent = PendingIntent.getActivity(this100,  
  87.                 intent, 0);  
  88.         // 3构建通知  
  89.         Notification.Builder builder = new Notification.Builder(this)  
  90.         // API 11添加的方法  
  91.                 .setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)  
  92.                 // 设置状态栏的小标题  
  93.                 .setLargeIcon(  
  94.                         BitmapFactory.decodeResource(getResources(),  
  95.                                 R.drawable.jay))// 设置下拉列表里的图标  
  96.                 .setWhen(System.currentTimeMillis()).setTicker("凤姐来啦")// 设置状态栏的显示的信息  
  97.                 .setAutoCancel(true)// 设置可以清除  
  98.                 .setContentTitle("通知通知"// 设置下拉列表里的标题  
  99.                 .setContentText("凤姐即将光临天拓游戏,各部门做好防雷准备"); // 设置可以清除  
  100.         Notification notification = builder.build();// API 16添加创建notification的方法  
  101.         // 通知  
  102.         manager.notify(110, notification);  
  103.   
  104.         // // 2构建通知  
  105.         // Notification notification2 = new Notification(R.drawable.jay, "天拓游戏",  
  106.         // System.currentTimeMillis());  
  107.         //  
  108.         // // 3设置通知的点击事件  
  109.         // Intent intent2 = new Intent(this, MainActivity.class);  
  110.         // PendingIntent contentIntent2 = PendingIntent.getActivity(this, 100,  
  111.         // intent2, 0);  
  112.         // notification2.setLatestEventInfo(this, "天拓游戏", "天拓游戏有个技术部",  
  113.         // contentIntent2);  
  114.         //  
  115.         // notification2.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失  
  116.         //  
  117.         // // 4发送通知  
  118.         // manager.notify(100, notification2);  
  119.     }  
  120. }  


稍微提一下的是,我们都知道Android SDK版本的变迁,API也会跟着遍,每个版本的API都可能会增删改一些接口,我们在使用Android为我们开发者提供的一些方法的时候,需要注意版本之间的区别,假如我们使用高版本的API的话,可能会引起一些错误,低版本的话可能在开发中又不适用了,反正程序不崩溃是最起码的保证,至于程序的功能能实现用什么API都是可以的。

0 0
原创粉丝点击