标签:android blog class code img com div java size http width
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-自定义Notification,码迷,mamicode.com
标签:android blog class code img com div java size http width
原文地址:http://blog.csdn.net/wwj_748/article/details/24559949