android添加通知到顶部任务栏
16lz
2021-01-23
public class NotificationtestActivity extends Activity { private static final int ID = 1213; private static final String KEY_COUNT="keyCount"; private int count; @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() {@Overridepublic void onClick(View v) {AddNotification();NotificationtestActivity.this.finish();}}); } /** * 添加顶部通知 * @author liuzhao */ public void AddNotification(){ count++;//添加通知到顶部任务栏//获得NotificationManager实例String service = NOTIFICATION_SERVICE;NotificationManager nm = (NotificationManager)getSystemService(service);//实例化NotificationNotification 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; //自动终止//实例化IntentIntent 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); }}
更多相关文章
- android --- 2g/3g流量问题实例
- android时间控件DatePicker使用实例
- 基于Android的短信管理系统
- 在android中创建包含对象数组对象List 的Parcelable
- Android View系统解析(上)
- android解析二维数组对象key:value
- android WebView 应用内点击超链接不调用系统浏览器
- Android 系统搜索框(有浏览记录)
- (备忘)Android app中调用启动其他应用(系统应用和第三方应用)