【Android】利用服务Service创建标题栏通知
创建标题栏通知的核心代码
public void CreateInform() {
//定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等)
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//创建一个通知
Notification notification = new Notification(R.drawable.icon, "巴拉巴拉~~", System.currentTimeMillis());
notification.setLatestEventInfo(context, "点击查看", "点击查看详细内容", pendingIntent);
//用NotificationManager的notify方法通知用户生成标题栏消息通知
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.notify(100, notification);//id是应用中通知的唯一标识
//如果拥有相同id的通知已经被提交而且没有被移除,该方法会用更新的信息来替换之前的通知。
}
全部Service代码
package com.app.myservice; import org.json.JSONException;
import org.json.JSONObject; import com.app.util.MyApplication; import android.R.integer;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder; public class ServiceDemo02 extends Service{
Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
context = getApplicationContext();
}
//创建通知
public void CreateInform() {
//定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等)
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); //创建一个通知
Notification notification = new Notification(R.drawable.icon, "巴拉巴拉~~", System.currentTimeMillis());
notification.setLatestEventInfo(context, "点击查看", "点击查看详细内容", pendingIntent); //用NotificationManager的notify方法通知用户生成标题栏消息通知
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.notify(100, notification);//id是应用中通知的唯一标识
//如果拥有相同id的通知已经被提交而且没有被移除,该方法会用更新的信息来替换之前的通知。
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stubm
super.onStart(intent, startId);
CreateInform();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} }
效果图
【Android】利用服务Service创建标题栏通知的更多相关文章
- android之服务service
service的概念: 官方定义:在后台长时间的操作,没有用户界面,不与用户进行交互,在默认的情况下,service运行在应用程序进程的主线程中,如果需要在Service中处理一些网络连接等耗时操作, ...
- Android系统编程入门系列之加载服务Service
之前几篇文章简单梳理了在Android系统的四大组件之一,最主要的界面Activity中,使应用程序与用户进行交互响应的相关知识点,那对于应用程序中不需要与用户交互的逻辑,又要用到哪些内容呢?本文开始 ...
- Android -- service 服务的创建与使用,生命周期,电话监控器
1. 为什么使用service 应用程序 : 一组组件(activity service provider receiver)的集合. 一般情况 一个应用程序 会对应一个进程. 一般情况 关闭掉应用 ...
- Android服务——Service
服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...
- Android服务Service
安卓Service服务 一 Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...
- C# Windows Service服务的创建和调试
前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...
- Android学习笔记--服务(Service)
1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...
- android服务Service(上)- IntentService
Android学习笔记(五一):服务Service(上)- IntentService 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服 ...
- android 后台服务定时通知
最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知. 1.首先我们需要用到Service类. 先上代码在慢慢解释 package ...
随机推荐
- u-boot 源码修改 bootcmd,IP ,BOOTARGS等参数
uboot1.1.6\include\configs\smdk6410.h #define CONFIG_BOOTCOMMAND"nand read 0xc0008000 0x200000 ...
- HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632
http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...
- DataTable转List<Model>通用类【实体转换辅助类】
/// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...
- 从表中随机返回n条记录
创建测试用表: CREATE OR REPLACE VIEW V AS SELECT 'a' AS c FROM dual UNION ALL SELECT 'b' AS c FROM dual UN ...
- XNA Game Studio 4.0 Programming 随便读,随便记 “Rendering Text”
在游戏中,通常要与玩家交流,比如分数,提示等.这就需要一种新的精灵, 它就叫SpritFont.下面先将其增加到项目中: 项目 -----> 添加新项-----> Sprite Font ...
- data audit on hadoop fs
最近项目中遇到了存储在HDFS上的数据格式不对,是由于数据中带有\r\n的字符,程序处理的时候没有考虑到这些情况.历史数据大概有一年的时间,需要把错误的数据或者重复的数据给删除了,保留正确的数据,项目 ...
- lamda表达式相关知识
lamda表达式写法 dt = datado.SelectDalMeath(sqlStr.ToString()); var x = (from r in dt.AsEnumerable() selec ...
- Window下开发React-Native Android步骤
1.安装Android开发环境 下载并安装JDK 下载并安装Android SDK, Android NDK 启动SDK下面的SDK Manager.exe,安装相关SDK Platform-tool ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- [5] Zygote
Android设备中的两大进程,如下图 1,由init进程创建的Daemon进程 2,由 Zygote进程创建的应用程序进程 什么是Zygote? zygote是“受精卵”的意思.在Android里, ...