MainActivity.java:

package com.example.notificationdemo;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build; public class MainActivity extends Activity {
Button btn_cancel;
Button btn_generate;
Notification notification;
PendingIntent pintent;
Intent intent;
NotificationManager manager;// 后面要用 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_generate = (Button) findViewById(R.id.btn_generate);
btn_generate.setOnClickListener(new MyListener());
btn_cancel.setOnClickListener(new MyListener()); } public class MyListener implements OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_generate:
System.out.println("=========btn_generate=====");
GenerateNotification();
break;
case R.id.btn_cancel:
System.out.println("=========btn_cancel=====");
CancelNotification();
break; }
} } private void GenerateNotification() { intent = new Intent(this, SecondActivity.class);
pintent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("这是手记状态栏提示");
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle("woshi biaoti");
builder.setContentText("标题内容我是");
builder.setContentIntent(pintent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setDefaults(Notification.DEFAULT_LIGHTS);
// builder.getNotification();//4.0以及以下版本用这个获取notification
Notification notification = builder.build();// 4.1以及以上版本用这个
Toast.makeText(this, "生成通知", 50).show();
manager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
manager.notify(23, notification);// 发出通知
} private void CancelNotification() {
// if(manager.equals("")||manager==null){//不能这么判断,因为加入manager为空,那么null.equals("")肯定就会空指针异常
if (manager == null) {
Toast.makeText(this, "亲,暂时没有消息,所以无法取消", 50).show();
return;
} else {
Toast.makeText(this, "取消", 50).show();
manager.cancel(23);
} }
}

  

  SecondActivity.java

package com.example.notificationdemo;

import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_act);
} }

  activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationdemo.MainActivity"
tools:ignore="MergeRootFrame" > <Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="取消通知" /> <Button
android:id="@+id/btn_generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="生成通知"
android:layout_below="@+id/btn_cancel" /> </RelativeLayout>

  second_act.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationdemo.MainActivity"
tools:ignore="MergeRootFrame" > <TextView
android:id="@+id/tv01"
android:text="我是第二页"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>

  清單文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.notificationdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>

  效果圖:

遇到一个奇怪的事情:

如下:

package com.example.notificationdemo;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class SecondActivity extends Activity implements OnClickListener {
Button btn;
PendingIntent pintent;
Intent intent;
NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_act);
System.out.println("second:" + Thread.currentThread().getName());
Toast.makeText(this, "second:" + Thread.currentThread().getName(), 21)
.show();
btn = (Button) findViewById(R.id.sec_generate);
Toast.makeText(this, "this is the second,begin to go back", 12).show();
btn.setOnClickListener(this);
System.out.println("第二页的,btn is onclicked");
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sec_generate:
intent = new Intent(this,MainActivity.class);
pintent = PendingIntent.getActivity(this, 0, intent, 0);//跳回第一页用
Builder builder = new Builder(this);
builder.setContentText("京东啊啊啊");
builder.setContentTitle("GO BACK");
builder.setSmallIcon(R.drawable.ic_launcher);//不设置SmallICon就没法跳转
// Notification notification = builder.build();
builder.setTicker("我是ticker");
builder.setContentIntent(pintent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setDefaults(Notification.DEFAULT_LIGHTS);
Notification notification = builder.build();//一定要放在setContentIntent()后
manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Toast.makeText(this, "back to the mainactivity", 12).show();
manager.notify(1, notification);
System.out.println("=============se===========");
break;
}
} }

  其中的builder.setSmallIcon(R.drawable.ic_launcher);如果注释掉,那么就不会生成相应的通知消息。暂时没找到原因,不知道为什么。

Android:Notification的生成与取消的更多相关文章

  1. android通知栏Notification点击,取消,清除响应事件

    主要是检测android通知栏的三种状态的响应事件 这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理 ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  5. eclipse下Android无法自动生成apk文件怎么办?

    eclipse下Android无法自动生成apk文件怎么办? 现象:创建android工程后,通过手动build/clean或自动build均无法在bin文件夹下生成.apk文件 解决方法:进入win ...

  6. 3、android notification 详细用法

    在 android 系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如: * 当保存文件等事件完成,应该会出现一个小的消息,以确认保存成功. * 如果应用程序在后台 ...

  7. android notification 传值关键

    android notification 传值关键在 onNewIntent方法里获取 @Override protected void onCreate(Bundle savedInstanceSt ...

  8. 【Android 基础】Android中全屏或者取消标题栏

    先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在se ...

  9. Android之自定义生成彩色二维码

    先导个zxing.jar包 下面是xml布局 activity_main.xml <RelativeLayout xmlns:android="http://schemas.andro ...

随机推荐

  1. ping的意思

    Ping是测试网络联接状况以及信息包发送和接收状况非常有用的工具,是网络测试最常用的命令.Ping向目标主机(地址)发送一个回送请求数据包,要求目标主机收到请求后给予答复,从而判断网络的响应时间和本机 ...

  2. Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 2 -使用XQuery 查询XML数据

    XQuery 是一个浏览/返回XML实例的标准语言. 它比老的只能简单处理节点的XPath表达式更丰富. 你可以同XPath一样使用.或是遍历所有节点,塑造XML实例的返回等. 作为一个查询语言, 你 ...

  3. Java调用Oracle存储Package

    Oracle的包Package中可以有很多存储,可通过该包的总调入口在java中直接调用. //java调用oracle的package代码 public boolean cal() throws j ...

  4. ubuntu之安装java浏览器插件

    最近搞什么openstack,在浏览器访问远程虚拟机的时候,需要浏览器有支持java.这个之前真没注意过呢, 通过自己的实践写点东西,方便一下你们搞: 1,首先去http://www.java.com ...

  5. C#用正则表达式 获取网页源代码标签的属性或值

    1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...

  6. HTML5 总结-应用程序缓存-8

    HTML 5 应用程序缓存 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(Application Cache)? HTM ...

  7. hdoj 2222

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道 AC自动机.....trie树的建立 和 AC自动机的查询,,可作模版... 解题思路:AC的应用 ...

  8. javascript 检测密码强度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. android 修改背景色(转)

    修改为黑底白字 修改AndroidManifest.xml把android:theme="@style/AppTheme" 修改为android:theme="@andr ...

  10. js获取控件位置

    //获取坐标位置 function getpos(e) { var t=e.offsetTop; var l=e.offsetLeft; var height=e.offsetHeight; whil ...