MainActivity.java:

  1. package com.example.notificationdemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.ActionBar;
  5. import android.app.Fragment;
  6. import android.app.Notification;
  7. import android.app.Notification.Builder;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.view.LayoutInflater;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.view.ViewGroup;
  18. import android.widget.Button;
  19. import android.widget.Toast;
  20. import android.os.Build;
  21.  
  22. public class MainActivity extends Activity {
  23. Button btn_cancel;
  24. Button btn_generate;
  25. Notification notification;
  26. PendingIntent pintent;
  27. Intent intent;
  28. NotificationManager manager;// 后面要用
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. btn_cancel = (Button) findViewById(R.id.btn_cancel);
  35. btn_generate = (Button) findViewById(R.id.btn_generate);
  36. btn_generate.setOnClickListener(new MyListener());
  37. btn_cancel.setOnClickListener(new MyListener());
  38.  
  39. }
  40.  
  41. public class MyListener implements OnClickListener {
  42.  
  43. @Override
  44. public void onClick(View v) {
  45. switch (v.getId()) {
  46. case R.id.btn_generate:
  47. System.out.println("=========btn_generate=====");
  48. GenerateNotification();
  49. break;
  50. case R.id.btn_cancel:
  51. System.out.println("=========btn_cancel=====");
  52. CancelNotification();
  53. break;
  54.  
  55. }
  56. }
  57.  
  58. }
  59.  
  60. private void GenerateNotification() {
  61.  
  62. intent = new Intent(this, SecondActivity.class);
  63. pintent = PendingIntent.getActivity(this, 0, intent, 0);
  64. Builder builder = new Builder(this);
  65. builder.setSmallIcon(R.drawable.ic_launcher);
  66. builder.setTicker("这是手记状态栏提示");
  67. builder.setWhen(System.currentTimeMillis());
  68. builder.setContentTitle("woshi biaoti");
  69. builder.setContentText("标题内容我是");
  70. builder.setContentIntent(pintent);
  71. builder.setDefaults(Notification.DEFAULT_SOUND);
  72. builder.setDefaults(Notification.DEFAULT_LIGHTS);
  73. // builder.getNotification();//4.0以及以下版本用这个获取notification
  74. Notification notification = builder.build();// 4.1以及以上版本用这个
  75. Toast.makeText(this, "生成通知", 50).show();
  76. manager = (NotificationManager) this
  77. .getSystemService(NOTIFICATION_SERVICE);
  78. manager.notify(23, notification);// 发出通知
  79. }
  80.  
  81. private void CancelNotification() {
  82. // if(manager.equals("")||manager==null){//不能这么判断,因为加入manager为空,那么null.equals("")肯定就会空指针异常
  83. if (manager == null) {
  84. Toast.makeText(this, "亲,暂时没有消息,所以无法取消", 50).show();
  85. return;
  86. } else {
  87. Toast.makeText(this, "取消", 50).show();
  88. manager.cancel(23);
  89. }
  90.  
  91. }
  92. }

  

  SecondActivity.java

  1. package com.example.notificationdemo;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class SecondActivity extends Activity {
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.second_act);
  12. }
  13.  
  14. }

  activity_main.xml:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/container"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.notificationdemo.MainActivity"
  7. tools:ignore="MergeRootFrame" >
  8.  
  9. <Button
  10. android:id="@+id/btn_cancel"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentRight="true"
  15. android:text="取消通知" />
  16.  
  17. <Button
  18. android:id="@+id/btn_generate"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentLeft="true"
  22. android:layout_alignParentRight="true"
  23. android:text="生成通知"
  24. android:layout_below="@+id/btn_cancel" />
  25.  
  26. </RelativeLayout>

  second_act.xml:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/container"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.notificationdemo.MainActivity"
  7. tools:ignore="MergeRootFrame" >
  8.  
  9. <TextView
  10. android:id="@+id/tv01"
  11. android:text="我是第二页"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. />
  15. </RelativeLayout>

  清單文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.notificationdemo"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="16"
  9. android:targetSdkVersion="19" />
  10. <uses-permission android:name="android.permission.FLASHLIGHT"/>
  11. <uses-permission android:name="android.permission.VIBRATE"/>
  12.  
  13. <application
  14. android:allowBackup="true"
  15. android:icon="@drawable/ic_launcher"
  16. android:label="@string/app_name"
  17. android:theme="@style/AppTheme" >
  18. <activity
  19. android:name="com.example.notificationdemo.MainActivity"
  20. android:label="@string/app_name" >
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23.  
  24. <category android:name="android.intent.category.LAUNCHER" />
  25. </intent-filter>
  26. </activity>
  27. <activity
  28. android:name="com.example.notificationdemo.SecondActivity"
  29. android:label="@string/app_name" >
  30. </activity>
  31. </application>
  32.  
  33. </manifest>

  效果圖:

遇到一个奇怪的事情:

如下:

  1. package com.example.notificationdemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.Notification;
  5. import android.app.Notification.Builder;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14.  
  15. public class SecondActivity extends Activity implements OnClickListener {
  16. Button btn;
  17. PendingIntent pintent;
  18. Intent intent;
  19. NotificationManager manager;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.second_act);
  24. System.out.println("second:" + Thread.currentThread().getName());
  25. Toast.makeText(this, "second:" + Thread.currentThread().getName(), 21)
  26. .show();
  27. btn = (Button) findViewById(R.id.sec_generate);
  28. Toast.makeText(this, "this is the second,begin to go back", 12).show();
  29. btn.setOnClickListener(this);
  30. System.out.println("第二页的,btn is onclicked");
  31. }
  32.  
  33. @Override
  34. public void onClick(View v) {
  35. switch (v.getId()) {
  36. case R.id.sec_generate:
  37. intent = new Intent(this,MainActivity.class);
  38. pintent = PendingIntent.getActivity(this, 0, intent, 0);//跳回第一页用
  39. Builder builder = new Builder(this);
  40. builder.setContentText("京东啊啊啊");
  41. builder.setContentTitle("GO BACK");
  42. builder.setSmallIcon(R.drawable.ic_launcher);//不设置SmallICon就没法跳转
  43. // Notification notification = builder.build();
  44. builder.setTicker("我是ticker");
  45. builder.setContentIntent(pintent);
  46. builder.setDefaults(Notification.DEFAULT_SOUND);
  47. builder.setDefaults(Notification.DEFAULT_LIGHTS);
  48. Notification notification = builder.build();//一定要放在setContentIntent()后
  49. manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
  50. Toast.makeText(this, "back to the mainactivity", 12).show();
  51. manager.notify(1, notification);
  52. System.out.println("=============se===========");
  53. break;
  54. }
  55. }
  56.  
  57. }

  其中的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. USB设备在连接PC时的reset从何而来?

    近期在做烧写工具的优化工作,有一些关于USB的内容须要总结一下当中包含设备的初始化过程和枚举过程. 在枚举的过程中,设备会一直等PC端的状态,当等到reset命令时会对设备进行又一次枚举.可是这个re ...

  2. c 整数的逆序输出 输入3,2就算 2+22+222的结果

    #include<stdio.h> #include<math.h> //整数逆序输出 void nixu() { int num,i; i = ; scanf("% ...

  3. iPhone 真机调试应用程序

    原文:http://blog.sina.com.cn/s/blog_68e753f70100r3w5.html 真机调试iphone应用程序 1.真机调试流程概述 1)       真机调试应用程序, ...

  4. [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)

    介绍 在本文中我们将利用SSIS参数,变量 以及 Foreach Loop Container 从多个源动态导入数据. 开始前我们先下载一些数据.WeatherData_Dec08_Apr09.zip ...

  5. WCF跟踪分析 使用(SvcTraceViewer)

    1.首先在WCF服务端配置文件中配置两处,用于记录WCF调用记录! A:<system.serviceModel>目录下: <diagnostics>      <mes ...

  6. Sublime Text 2使用技巧汇总

    一.下载链接: Windows-64bit: http://pan.baidu.com/s/1o6QdKYu 其它版本请移步官网: http://www.sublimetext.com/ 二.破解Li ...

  7. CURL采集

    <?php $url='';//输入'网址 $ch = curl_init(); $timeout = 5;//超时时间 curl_setopt ($ch, CURLOPT_URL, $url) ...

  8. 高质量程序设计指南C/C++语言——C++/C常量

  9. Qt Installer Framework的学习

    Qt Installer Framework是Qt默认包的发布框架.它很方便,使用静态编译Qt制作而成.并且使用了压缩率很高的7z对组件进行压缩.之所以有这些好处,我才觉得值得花一点儿精力研究一下这个 ...

  10. notify()、notifyAll()和wait()

    看到一道面试题,写一个多线程程序,交替输出1.2.1.2…… 先写下程序: /** * Created by Andrew on 2015/10/28. */ public class OutputT ...