程序发送和取消状态栏的通知
来源:互联网 发布:淘宝申请退货时间 编辑:程序博客网 时间:2024/06/16 09:57
代码:
package com.xie.app;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {//布局中两个buttonprivate Button btn1, btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button) findViewById(R.id.button1);btn2 = (Button) findViewById(R.id.button2);//设置监听btn1.setOnClickListener(this);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {//通知信息的id,用来标示通知int id = 1;//通知管理NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (v == btn1) {//创建一个通知对象,参数:通知显示的图标,通知开始活动时显示的内容,通知上显示的时间[通知排序通过这个]Notification notification = new Notification(R.drawable.ic_launcher, "第一次看到显示",System.currentTimeMillis());//用户点击通知时自动消失notification.flags = Notification.FLAG_AUTO_CANCEL;//通知显示在正在运行那一栏中:FLAG_ONGOING_EVENT 表示运行在正在运行那一栏中 FLAG_NO_CLEAR 不能在通知栏上清除//notification.flags |= Notification.FLAG_ONGOING_EVENT;//notification.flags |= Notification.FLAG_NO_CLEAR;//创建一个intent,如果程序已经运行那么找到入口进入,不然重新打开Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);//设置component,用于找哪个程序和主页面intent.setComponent(new ComponentName(this, MainActivity.class));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//创建PendingIntent对象PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, 0);//设置最后在状态栏显示的内容和点击后的pendingIntentnotification.setLatestEventInfo(this, "标题", "通知内容", pendingIntent);//通过id发送通知manager.notify(id, notification);}else{//通过id取消通知manager.cancel(id);}}}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="29dp" android:layout_marginTop="85dp" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="32dp" android:layout_toRightOf="@+id/button1" android:text="Button" /></RelativeLayout>
- 程序发送和取消状态栏的通知
- 使用Notification实现状态通知栏的发送和取消
- 如何取消状态栏上显示所有的图标和通知的勾选?
- 发送状态栏通知 Notification
- android 发送状态栏通知
- android-发送状态栏通知
- 状态栏发送通知
- 安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知
- iOS开发之旅--本地通知的发送与取消
- 将程序放到通知区,并隐藏程序和状态栏
- 取消标题和状态栏
- iOS10通知(三)--通知的取消和修改
- android开发之向状态栏发送通知
- Android发送一个通知到状态栏去
- iOS10通知(四)--带事件通知的发送和处理
- 显示通知:Toast和状态栏通知
- 笔记9 Notification Notification创建、发送通知及取消通知
- iphone 设置程序运行取消电池状态栏
- 网络七层
- 利用Heritrix构建特定站点爬虫
- 分析Petshop4.0的数据库【转载网页】
- LDD3 scull pipe 模块
- 面试问题系列:HR面试与压力面试
- 程序发送和取消状态栏的通知
- Eclipse:批量将Java源代码文件的编码从GBK转为UTF-8
- sql server竟然可以有办法不让事件探测器显示具体的sql
- 在linux中用窗口参看文件时,如何显示当前的路径
- Exchange Server各版本OWA架构差异
- JDBC的事务控制
- delphi中关键字inherited
- KVC与KVO:
- The App Name you entered has already been used