标签:
public class MainActivity extends AppCompatActivity {
public static TextView tvPayload ;
public static final int NOTIFICATION_API11 = 1;//notificationId
public static final int NOTIFICATION_API16 = 2;
public static final int NOTIFICATION_CUSTOM = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 在布局文件中为每个按钮设置了onclick="click"
*/
public void click(View v){
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/**
* 设置意图,指向另一个activity,当点击notification的时候,会跳转过去
* action = "com.xiaohua.notification.MSG"
* category = "android.intent.category.DEFAULT"
*/
Intent intent = new Intent();
intent.setAction("com.xiaohua.notification.MSG");
intent.addCategory("android.intent.category.DEFAULT");
// 告诉activity使用自己软件中的任务站管理activity,如果没有这个任务栈,系统会帮它创建一个(此设置可有可无)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
switch (v.getId()){
case R.id.btn_notify11://编译版本大于11,小于16使用这种方法
/**
* 创建一个PendingIntent ,和intent类似,由于不是马上调用,需要下拉状态发出的activity,所以采用的是PendingIntent,即点击Notification跳转到哪个acitivity
* context 上下文
* requestCode 请求码
* intent 打开其他界面的意图
* flags 用来标识延迟意图是否只能使用一次:FLAG_ONE_SHOT只能使用一次
*/
intent.putExtra("msg","XXX,我想你了");
PendingIntent pendingIntent11 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notify11 = new Notification.Builder(this)
.setSmallIcon(R.drawable.notify_icon)//设置状态栏的小图片,也是在下拉状态栏中所显示,如果需要更换更大的图片,可以使用setlargeIcon(Bitmap icon)
.setContentTitle("新消息title")//textview中显示的标题
.setContentText("XXX,我想你了")//Textview中显示的详细内容
.setContentIntent(pendingIntent11)//关联PendingIntent
.setNumber(3)//在Textview的右方显示的数字
.getNotification();//builder是在API level 16及以后增加的,在API11-API15可以使用getNotification()来代替
notify11.flags = Notification.FLAG_AUTO_CANCEL;//通知显示后自动消失
notify11.defaults = Notification.DEFAULT_ALL;//设置通知其他选项:使用系统默认值:声音、震动、亮度
manager.notify(NOTIFICATION_API11,notify11);
break;
case R.id.btn_notify16:
// intent = new Intent(this,Notify_16_Activity.class);
intent.putExtra("msg","这是在API 16下创建的notify跳转的界面");
PendingIntent pendingIntent16 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notify16 = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notify_16_icon)
.setTicker("TickerText:你有新消息")//状态栏提示消息时显示的内容
.setContentTitle("消息title")
.setContentText("消息content")
.setContentIntent(pendingIntent16)//消息的延迟意图
.setNumber(2)
.build();
notify16.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_API16,notify16);
break;
case R.id.btn_mynotify:
intent.putExtra("msg","我是从自定义的notity跳转过来的");
Notification myNotify = new Notification();
myNotify.icon = R.drawable.notify_custom;
myNotify.tickerText = "我的自定义消息";
myNotify.when = System.currentTimeMillis();//设置notification显示的时间
myNotify.flags = Notification.FLAG_NO_CLEAR;//不自动清除
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_custom);
remoteViews.setTextViewText(R.id.tv_title,"自定义notify来了");
remoteViews.setImageViewResource(R.id.iv_icon,R.drawable.notify_custom);//设置通知栏中的内容
// remoteViews.setImageViewBitmap(int viewId,Bitmap bitmap);
// remoteViews.setImageViewUri(int viewId, Uri uri);
myNotify.contentView = remoteViews;//给notification设置自定义的布局
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
myNotify.contentIntent = pendingIntent;
manager.notify(NOTIFICATION_CUSTOM,myNotify);
break;
case R.id.btn_clear11:
manager.cancel(NOTIFICATION_API11);//根据notification的id号清除消息
break;
case R.id.btn_clear16:
manager.cancel(NOTIFICATION_API16);
break;
case R.id.btn_clear_custom:
manager.cancel(NOTIFICATION_CUSTOM);
break;
case R.id.btn_clear:
manager.cancel(NOTIFICATION_API11);
manager.cancel(NOTIFICATION_API16);
manager.cancel(NOTIFICATION_CUSTOM);
break;
default:
break;
}
}
}
标签:
原文地址:http://blog.csdn.net/u013578042/article/details/51358274