zoukankan      html  css  js  c++  java
  • Android-它们的定义Notification

    Android-它们的定义Notification


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

    先来看看效果图:
    有兴趣的朋友能够加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)


    上面就是通知栏的效果了,我们主要改的地方有大头像。小头像,标题,内容等,直接看代码:
    package com.wwj.custom.notification;
    
    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.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    /**
     * 自己定义Notification
     * 
     * @author wwj
     *
     */
    public class MainActivity extends Activity implements OnClickListener {
    
    	private Button showNotification;
    	private Button showCustomNotifi;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		showNotification = (Button) findViewById(R.id.button1);
    		showCustomNotifi = (Button) findViewById(R.id.button2);
    
    		showNotification.setOnClickListener(this);
    		showCustomNotifi.setOnClickListener(this);
    
    	}
    
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
    		case R.id.button1:
    			send();
    			break;
    		case R.id.button2:
    			custom();
    			break;
    
    		default:
    			break;
    		}
    	}
    
    	/**
    	 * 旧方法
    	 */
    	public void send() {
    		// 1 得到通知管理器
    		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    		// 2构建通知
    		Notification notification = new Notification(
    				android.R.drawable.stat_notify_chat, "这是提示信息",
    				System.currentTimeMillis());
    
    		// 3设置通知的点击事件
    		Intent intent = new Intent(this, MainActivity.class);
    		PendingIntent contentIntent = PendingIntent.getActivity(this, 100,
    				intent, 0);
    		notification.setLatestEventInfo(this, "通知的标题", "通知的内容", contentIntent);
    
    		notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自己主动消失
    
    		// 4发送通知
    		nm.notify(100, notification);
    	}
    
    	/**
    	 * 自己定义Notification 新方法
    	 * 新的方法,本人在手机測试会崩溃,假设不行的话,能够继续使用旧的构建方法,毕竟高版本号会兼容低版本号的
    	 */
    	public void custom() {
    		// 1 得到通知管理器
    		NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    		// 2 设置通知的点击事件
    		Intent intent = new Intent(this, MainActivity.class);
    		PendingIntent contentIntent = PendingIntent.getActivity(this, 100,
    				intent, 0);
    		// 3构建通知
    		Notification.Builder builder = new Notification.Builder(this)
    		// API 11加入的方法
    				.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)
    				// 设置状态栏的小标题
    				.setLargeIcon(
    						BitmapFactory.decodeResource(getResources(),
    								R.drawable.jay))// 设置下拉列表里的图标
    				.setWhen(System.currentTimeMillis()).setTicker("凤姐来啦")// 设置状态栏的显示的信息
    				.setAutoCancel(true)// 设置能够清除
    				.setContentTitle("通知通知") // 设置下拉列表里的标题
    				.setContentText("凤姐即将光临天拓游戏,各部门做好防雷准备"); // 设置能够清除
    		Notification notification = builder.build();// API 16加入创建notification的方法
    		// 通知
    		manager.notify(110, notification);
    
    		// // 2构建通知
    		// Notification notification2 = new Notification(R.drawable.jay, "天拓游戏",
    		// System.currentTimeMillis());
    		//
    		// // 3设置通知的点击事件
    		// Intent intent2 = new Intent(this, MainActivity.class);
    		// PendingIntent contentIntent2 = PendingIntent.getActivity(this, 100,
    		// intent2, 0);
    		// notification2.setLatestEventInfo(this, "天拓游戏", "天拓游戏有个技术部",
    		// contentIntent2);
    		//
    		// notification2.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自己主动消失
    		//
    		// // 4发送通知
    		// manager.notify(100, notification2);
    	}
    }
    


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








    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    springmvc&参数绑定&异常处理&Json数据交互&上传图片
    从零开始搭建一个vue.js的脚手架
    supervisor配置详解(转)
    supervisor 从安装到使用
    linux下如何查看某软件是否已安装
    linux yum命令详解
    Vue.js 和 MVVM 小细节
    PHP7的异常处理机制,set_error_handler和set_exception_handler方法介绍
    使用 acme.sh 签发续签 Let‘s Encrypt 证书 泛域名证书
    SSH原理与运用(一):远程登录
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4658507.html
Copyright © 2011-2022 走看看