一般当我们收到短信啊,微信啊,或者有些app的提醒。我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,事实上android中有专门的Notification的类能够完毕这个工作,这里就实现下这个功能。

首先新建NotificationTestproject,然后加入一个button,用来触发通知。然后编写代码例如以下:

package com.example.jared.notificationtest;import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private Button sendNotificationBtn;private int mId = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendNotificationBtn = (Button)findViewById(R.id.sendNotification);sendNotificationBtn.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.sendNotification:setSendNotificationBtn();break;default:break;}}}public void setSendNotificationBtn () {NotificationCompat.Builder notification = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("My Notification").setContentText("Hello Notification");NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.notify(mId, notification.build());}
}

这里了用了NotificatonCompat.Builder来创建一个简单的Notification,setSmallIcon是指定当中的图标,setContentTitle方法是指定标题,setContentText指定内容,然后通过getSystemService获取通知的管理类,通过notify方法发送通知,当中mId是一个id号,每个通知有其独特的通知号。不能反复。

执行效果例如以下所看到的:

接着我们来实现点击通知后跳转到相应的Activity中,然后消除这条通知。再创建一个Activity,布局例如以下:

<?xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jared.notificationtest.Notification"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎点击通知事件!" android:layout_margin="20dp" android:textSize="20dp"/> </LinearLayout>

这里就一个textview用来显示下信息,接着编写代码例如以下:

package com.example.jared.notificationtest;import android.app.NotificationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;public class Notification extends AppCompatActivity {private int mId = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification);NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);manager.cancel(mId);}
}

这里进入到Activity后就把通知清除掉,接着就是改动MainActivity代码:

package com.example.jared.notificationtest;import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private Button sendNotificationBtn;private int mId = 1;private int numMessage = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendNotificationBtn = (Button)findViewById(R.id.sendNotification);sendNotificationBtn.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.sendNotification:setSendNotificationBtn();break;default:break;}}}public void setSendNotificationBtn () {NotificationCompat.Builder notification = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("My Notification").setContentText("Hello Notification").setNumber(++numMessage);Intent intent = new Intent(this, Notification.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);notification.setContentIntent(pendingIntent);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.notify(mId, notification.build());}
}

这里又加入了setNumber方法。主要是显示来了几条通知,比方微信中就须要知道,然后实例化了一个intent。再实例化一个pendingIntent。获取activity,在NotificationCompat.Builder里setContentIntent。之后就能够达到我们的效果,执行并点击通知例如以下所看到的:

       

如上所看到的收到了6条通知,然后点击后通知也消除了。

一般在下载歌曲啊。图片啊的时候。会有进度条表示下载的过程,这里来模拟实现下这个功能。改动MainAcitivy代码例如以下:

package com.example.jared.notificationtest;import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private Button sendNotificationBtn;private int mId = 1;private int numMessage = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendNotificationBtn = (Button)findViewById(R.id.sendNotification);sendNotificationBtn.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.sendNotification:setSendNotificationBtn();break;default:break;}}}public void setSendNotificationBtn () {final NotificationCompat.Builder notification = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Music Download").setContentText("burning.mp3").setNumber(++numMessage);Intent intent = new Intent(this, Notification.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);notification.setContentIntent(pendingIntent);final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);new Thread(new Runnable(){@Overridepublic void run() {for(int cnt=0; cnt<=100; cnt++){notification.setProgress(100, cnt, false);manager.notify(mId, notification.build());try {Thread.sleep(200);} catch (InterruptedException e) {Log.d(TAG, "Sleep failure");}}notification.setContentText("Download complete");notification.setProgress(0, 0, false);manager.notify(mId, notification.build());}}).start();}
}

这里通过setProgress方法来实现,这里开了一个Thread,当下载完毕后又一次设置下内容。

执行结果例如以下:

     

图1显示运行进度条在走,图2完毕了下载功能。

一般收到通知,手机都会有一段声音。加上震动。那么接下来来实现这个功能。上图,假设下载完毕后,就放一段音乐而且震动,改动代码例如以下:

package com.example.jared.notificationtest;import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;import java.io.File;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private Button sendNotificationBtn;private int mId = 1;private int numMessage = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendNotificationBtn = (Button)findViewById(R.id.sendNotification);sendNotificationBtn.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.sendNotification:setSendNotificationBtn();break;default:break;}}}public void setSendNotificationBtn () {final NotificationCompat.Builder notification = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Music Download").setContentText("burning.mp3").setNumber(++numMessage);Intent intent = new Intent(this, Notification.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);notification.setContentIntent(pendingIntent);final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);new Thread(new Runnable(){@Overridepublic void run() {for(int cnt=0; cnt<=100; cnt++){notification.setProgress(100, cnt, false);manager.notify(mId, notification.build());try {Thread.sleep(100);} catch (InterruptedException e) {Log.d(TAG, "Sleep failure");}}notification.setContentText("Download complete");notification.setProgress(0, 0, false);Uri soundUri = Uri.fromFile(new File("/system/media/audio/animationsounds/bootSound.ogg"));notification.setSound(soundUri);long[] vibrates = {0, 1000, 1000, 1000};notification.setVibrate(vibrates);manager.notify(mId, notification.build());}}).start();}
}

这里加上了setSound和setVibrate方法,而且须要在AndroidManifest中加入权限:

<uses-permission android:name="android.permission.VIBRATE"/>

这里的歌曲名是通过adb shell查看系统的存在的音乐:

下载到手机执行后就能够观察效果。

当然还能够控制led灯,不知为啥led灯的效果一直没有,网上翻阅非常多资料也没找到问题所在,若有朋友知道。麻烦告知一二不甚感激。

 notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));long[] vibrates = {0, 1000, 1000, 1000};notification.setVibrate(vibrates);notification.setLights(Color.GREEN, 1000, 1000);

关于Notification基本上就学到这里了。

Android开发学习之路--Notification之初体验相关推荐

  1. Android开发学习之路--Camera之初体验

    顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就能够通过camera来拍照,然后存储照片.发送给好友.那么微信的app里面是不会直接通过camera api来实现的,由于 ...

  2. android开发用百度识别图片格式,Android开发学习之路-机器学习库(图像识别)、百度翻译...

    对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...

  3. android开发学习之路——连连看之游戏逻辑(五)

    GameService组件则是整个游戏逻辑实现的核心,而且GameService是一个可以复用的业务逻辑类. (一)定义GameService组件接口 根据前面程序对GameService组件的依赖, ...

  4. android开发学习之路——连连看之加载图片(三)

    正如前面AbstractBoard类的代码中看到的,当程序需要创建N个Piece对象时,程序会直接调用ImageUtil的getPlayImages()方法去获取图片,该方法将会随机从res\ dra ...

  5. Android开发学习之路-环境搭建

    这里选择使用android studio 集成开发环境,因为as是google推出的单独针对android开发的环境,并且迭代周期很快,因此,肯定会替代eclipse成为andorid的开发环境.对于 ...

  6. Android开发学习之路--网络编程之初体验

    一般手机都是需要上网的,一般我们的浏览器就是个webview.这里简单实现下下功能,先编写Android的layout布局: <?xml version="1.0" enco ...

  7. Android开发学习之路--UI之简单聊天界面

    学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...

  8. android 程序等待时间,Android开发学习之路--性能优化之常用工具

    Android性能优化相关的开发工具有很多很多种,这里对如下六个工具做个简单的使用介绍,主要有Android开发者选项,分析具体耗时的Trace view,布局复杂度工具Hierarchy View, ...

  9. Android图片颜色比例,Android开发学习之路-图片颜色获取器开发(1)

    系列第一篇,从简单的开始,一步一步完成这个小项目. 颜色获取就是通过分析图片中的每个像素的颜色,来分析整个图片的主调颜色,有了主调颜色,我们可以用于图片所在卡片的背景或者标题颜色,这样整体感更加强烈. ...

最新文章

  1. 没有了SA密码,无法Windows集成身份登录,DBA怎么办?
  2. python官网下载安装教程-各种版本的Python下载安装教程
  3. Spring Boot中使用RabbitMQ
  4. 卡尔曼滤波器求速度matlab,卡尔曼滤波器算法浅析及matlab实战
  5. “城迷”:黑白梦与精神逃离
  6. 如何将世界时钟和时区小部件添加到您的iPhone
  7. LeetCode 542. 01 矩阵(BFS DP)
  8. php while 自增,PHP 布尔值的自增与自减的实现方法
  9. spacy model
  10. 蓝桥杯 ADV-126 算法提高 扫雷
  11. 清华大学2017届本科毕业典礼演讲——做有思想的行者
  12. NoSuchMethod: ByteBuffer.position(I)
  13. 流媒体压力测试工具—推拉流
  14. 迪普交换机恢复出厂设置_LSW交换机初始化配置指导
  15. Quartz表达式介绍及简单使用
  16. Revit导入lumion渲染
  17. oracle 电子书大全
  18. 数据库之查询表sc——计算1号课程的学生平均成绩。
  19. Curl POST to HTTPS url gives SSLRead() error:curl: (56) SSLRead() return error -9806
  20. [CISCN2019 华北赛区 Day2 Web1]Hack World

热门文章

  1. python软件管理系统_conda:基于python的软件管理系统
  2. 考勤排班_如何设置钉钉做2休1等考勤?
  3. python中outside loop_Python: 'break' outside loop
  4. Shell-流程控制案例
  5. vue中获取url参数
  6. MyBatis基于注解的使用
  7. Eclipse 导入 Tomcat 源码
  8. 找出如下数组中最大的元素和最小的元素, a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}}
  9. 【Zookeeper】Zookeeper部署笔记
  10. 使用花生壳自己架设网站:路由器后