在前台运行的 Activity 可以通过Dialog、Toast 向用户发出提示信息,而后台运行的程序,如下载、收到信息等 Service 应用,则需要使用 Notification(通知)向用户发出提示信息。

 import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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;
import android.widget.RemoteViews; public class NotificationActivity extends Activity { Button b1;
NotificationManager nmanager;
Notification notification;
int notificationID=1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.notificationlayout);
b1=(Button) findViewById(R.id.notification_bt1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//sendNotification();
sendCustomNotification();
}}
);
} // 发送自定义通知
public void sendCustomNotification(){
//1.获得 NotificationManager
nmanager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//2.创建 Notification
notification =new Notification(
R.drawable.folder_open,
"收到文件",
System.currentTimeMillis()
);
RemoteViews rv = new RemoteViews(getPackageName(),R.layout.notificationinterfacelayout);
rv.setImageViewResource(R.id.notification_img, R.drawable.savefile);
rv.setTextViewText(R.id.notification_title, "催眠曲.mp3");
rv.setProgressBar(R.id.notification_progressbar, 100, 20, false);
notification.contentView=rv;
//3.设置属性,这些属性会在展开状态栏后显示
Intent intent =new Intent(this,ToastActivity.class); //转向其他
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
notification.contentIntent=pIntent;
//4.将Notification发给Manager
nmanager.notify(notificationID++, notification);
} // 发送通知
public void sendNotification(){
//1.获得NotificationManager
nmanager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//2.创建Notification
notification =new Notification(
R.drawable.folder_open,
"收到文件",
System.currentTimeMillis()
);
// 可选属性
notification.defaults|=Notification.DEFAULT_SOUND;
notification.flags |=Notification.FLAG_INSISTENT; // 3.设置属性,这些属性会在展开状态栏后显示
Intent intent =new Intent(this,ToastActivity.class); //转向其他
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "接收文件", "文件已经下载完成", pIntent);
// 4.将 Notification 发给 Manager
nmanager.notify(notificationID++, notification);
} }

Android 开发工具类 16_NotificationActivity的更多相关文章

  1. Android开发工具类

    7种无须编程的DIY开发工具 你知道几个? 现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工 具的出现,也帮助 ...

  2. android开发工具类之获得WIFI IP地址或者手机网络IP

    有的时候我们需要获得WIFI的IP地址获得手机网络的IP地址,这是一个工具类,专门解决这个问题,这里需要两个权限: <uses-permission android:name="and ...

  3. android开发工具类总结(一)

    一.日志工具类 Log.java public class L { private L() { /* 不可被实例化 */ throw new UnsupportedOperationException ...

  4. Android 开发工具类 35_PatchUtils

    增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...

  5. Android 开发工具类 13_ SaxService

    网络 xml 解析方式 package com.example.dashu_saxxml; import java.io.IOException; import java.io.InputStream ...

  6. Android 开发工具类 06_NetUtils

    跟网络相关的工具类: 1.判断网络是否连接: 2.判断是否是 wifi 连接: 3.打开网络设置界面: import android.app.Activity; import android.cont ...

  7. Android 开发工具类 03_HttpUtils

    Http 请求的工具类: 1.异步的 Get 请求: 2.异步的 Post 请求: 3.Get 请求,获得返回数据: 4.向指定 URL 发送 POST方法的请求. import java.io.Bu ...

  8. Android 开发工具类 19_NetworkStateReceiver

    检测网络状态改变类: 1.注册网络状态广播: 2.检查网络状态: 3.注销网络状态广播: 4.获取当前网络状态,true为网络连接成功,否则网络连接失败: 5.注册网络连接观察者: 6.注销网络连接观 ...

  9. Android 开发工具类 27_多线程下载大文件

    多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...

随机推荐

  1. struts 拦截器

    my-default.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  2. C++中的浮点数运算的误差测试分析

    C++中的浮点数运算的误差 项目中需要计算判定,采用的是float型,如: float a < yLing, 其中:a = 2.0, y则从1.0 + 0.2*n  当n = 4时,条件成立了? ...

  3. 集合(五)不正确地使用HashMap引发死循环及元素丢失

    前一篇文章讲解了HashMap的实现原理,讲到了HashMap不是线程安全的.那么HashMap在多线程环境下又会有什么问题呢? 几个月前,公司项目的一个模块在线上运行的时候出现了死循环,死循环的代码 ...

  4. Intellij Idea 14编译golang 插件

    最近项目实在太赶了,很久没有写过博文了. 公司新配了一台笔记本电脑,原装win8的.于是又从linux回到了windows.不想用win命令行来搞go了,win下太折腾了.还是用一直使用的idea. ...

  5. java多线程 —— 两种实际应用场景模拟

    最近做的偏向并发了,因为以后消息会众多,所以,jms等多个线程操作数据的时候,对共享变量,这些要很注意,以防止发生线程不安全的情况. (一) 先说说第一个,模拟对信息的发送和接收.场景是这样的: 就像 ...

  6. AgentJob--无法重启Job--22022错误

    错误描述: 发现有个Job计划为长期重复运行,但从某个时间点后未运行,无任何错误信息. 手动启动该Job后爆以下错误 运行环境: Window Server 2008 R2 Enterprise Mi ...

  7. asp.net——公共帮助类

    在项目开发中公共帮助类是必不可少的,这里记录一些自己摘录或自己编写的帮助类. 64位编码与解码: #region URL的64位编码 /// <summary> /// URL的64位编码 ...

  8. ASP.NET Core 2 学习笔记(五)静态文件

    之前的ASP.NET网站,只要把*.html.*.css.*.jpg.*.png.*.js等静态文件放在项目根目录,默认都可以直接被浏览:但ASP.NET Core 小改了浏览静态文件的方式,默认根目 ...

  9. Day 11 函数名,闭包,装饰器. +作业

    '''一.函数名.def func(): print(5555)print(func)#输出结果 <function func at 0x026B5E88> 打印函数地址. # 1. 函数 ...

  10. python爬虫从入门到放弃(一)——试用bs4, request爬百度股票

    文章实践主要来自于:https://mp.weixin.qq.com/s/FiKqb06nz0K0AD9VUWJapw 爬虫流程: 明确目的(哪些数据),确认网页可爬,查看源网页是否有需要的数据. b ...