package com.notificationTest;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class NotificationtestActivity extends Activity {

private static final int ID = 1213;

private static final String KEY_COUNT="keyCount";

private int count;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Intent intent=this.getIntent();

count=intent.getIntExtra(KEY_COUNT,0);

this.setTitle("这是第"+Integer.toString(count)+"个");

Button btn=(Button) this.findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

AddNotification();

NotificationtestActivity.this.finish();

}

});

}

/**

* 添加顶部通知

* @author liuzhao

*/

public void AddNotification(){

count++;

//添加通知到顶部任务栏

//获得NotificationManager实例

String service = NOTIFICATION_SERVICE;

NotificationManager nm = (NotificationManager)getSystemService(service);

//实例化Notification

Notification n = new Notification();

//设置显示图标

int icon = R.drawable.icon;

//设置提示信息

String tickerText ="我的程序";

//显示时间

long when = System.currentTimeMillis();

n.icon = icon;

n.tickerText = tickerText;

n.when = when;

//显示在“正在进行中”

// n.flags = Notification.FLAG_ONGOING_EVENT;

n.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止

//实例化Intent

Intent it = new Intent(this,NotificationtestActivity.class);

it.putExtra(KEY_COUNT, count);

/*********************

*获得PendingIntent

*FLAG_CANCEL_CURRENT:

* 如果当前系统中已经存在一个相同的PendingIntent对象,

* 那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

*FLAG_NO_CREATE:

* 如果当前系统中不存在相同的PendingIntent对象,

* 系统将不会创建该PendingIntent对象而是直接返回null。

*FLAG_ONE_SHOT:

* 该PendingIntent只作用一次,

* 如果该PendingIntent对象已经触发过一次,

* 那么下次再获取该PendingIntent并且再触发时,

* 系统将会返回一个SendIntentException,在使用这个标志的时候一定要注意哦。

*FLAG_UPDATE_CURRENT:

* 如果系统中已存在该PendingIntent对象,

* 那么系统将保留该PendingIntent对象,

* 但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,

* 例如更新Intent中的Extras。这个非常有用,

* 例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,

* 达到在不同时机传递给MainActivity不同的参数,实现不同的效果。

*********************/

PendingIntent pi = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);

//设置事件信息,显示在拉开的里面

n.setLatestEventInfo(NotificationtestActivity.this,"我的软件"+Integer.toString(count), "我的软件正在运行……", pi);

//发出通知

nm.notify(ID,n);

}

}

更多相关文章

  1. 类和 Json对象
  2. Android--登录界面(demo)
  3. The android gradle plugin version 3.0.0-alpha1 is too old
  4. Android(安卓)SDK 更新失败(google相关网站被屏蔽)解决方法
  5. Android中调用摄像头拍照保存,并在相册中选择图片显示
  6. Android:实现定时器 Handler的postDelayed(Runnable, long)方法
  7. Android(安卓)Sdk Manager更新
  8. NestedScrollView嵌套RecyclerView最后一条item显示不全
  9. Android(安卓)PreferenceActivity的介绍

随机推荐

  1. 【Android】 导入项目报错的解决方案
  2. Android(安卓)菜单(OptionMenu)大全 建立
  3. Android(安卓)package属性、package name
  4. Android中ListView.getCount()与ListView
  5. Android中界面实现全屏显示的两种方式
  6. android散点技术
  7. Android横竖屏切换方法
  8. android的线程特点
  9. Android(安卓)Drawable系列(1):自定义背景以
  10. android全屏显示和android模拟器输入中文