在前台运行的 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. day07(Set接口,HashSet类,hashcoad(),Collections工具类,Map集合)

    Set接口 set接口的实现类特点 1.无序(取出来的顺序和存进去的数据的顺序不一致) 2.唯一(数据不能存相同的) 底层是用Map集合写的 HashSet类  实现了  set接口       唯一 ...

  2. android 热更新nuwa

    简介 Nuwa是比较流行的一种Android热补丁方案的开源实现,它的特点是成功率高,实现简单.当然,热补丁的方案目前已经有很多了,AndFix, Dexposed, Tinker等,之所以要分析Nu ...

  3. win7结束进程 时,提示“拒绝访问”、“没有此任务的实例运行”怎么办?

    开发了个程序,创建了一个进程,但是杀不掉了,在任务管理器里面 右键--结束进程,提示“拒绝访问”,或者“没有此任务实例运行” 怎么办? 直接给答案:PCHunter 具体方法: 1.打开PCHunte ...

  4. Latex中Matlab代码的环境

    需要用到listings宏包 使用方法: 导言区\usepackage{listings}\lstset{language=Matlab}      %代码语言使用的是matlab\lstset{br ...

  5. android应用搬家的实现

    android手机上的应用搬家功能,具体的介绍和原理参考: 系统目录及应用搬家的研究 应用搬家的实现 这里主要是作为一个补充,因为上面两篇文章虽然讲的挺好的,但是给出的例子不能直接运行,还是需要一些准 ...

  6. 关于Unity中的NavMeshAgent的remainingDistance问题

    Unity中的NavMeshAgent的remainingDistance问题 在Unity官方案例中,要让某个人物移动到某个地方,一般来说都是下面这样的代码: agent.SetDestinatio ...

  7. shiro之深度解析FormAuthenticationFilter

      shiro是我们在项目经常使用到的权限管理框架,本文我们就重点来分析FormAuthenticationFilter的验证过程. FormAuthenticationFilter 1.继承结构   ...

  8. linux ab压力测试工具及ab命令详解

    原文链接:https://blog.csdn.net/qq_27517377/article/details/78794409 yum -y install httpd-tools ab -v 查看a ...

  9. jzoj3363

    JYY 来到了一个新的城市,为了和大家保持联系,第一件事就是办理新的手机号.JYY 对号码的要求很高,希望大家都能够顺畅地阅读手机号,因此 JYY 特别研究了地球人的电话号码阅读习惯,有如下重大发现 ...

  10. 队列的实现——java

    同样实现方法有两种: 1. 数组的实现,可以存储任意类型的数据(略): 2. Java的 Collection集合 中自带的"队列"(LinkedList)的示例: import ...