创建标题栏通知的核心代码

	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创建标题栏通知的更多相关文章

  1. android之服务service

    service的概念: 官方定义:在后台长时间的操作,没有用户界面,不与用户进行交互,在默认的情况下,service运行在应用程序进程的主线程中,如果需要在Service中处理一些网络连接等耗时操作, ...

  2. Android系统编程入门系列之加载服务Service

    之前几篇文章简单梳理了在Android系统的四大组件之一,最主要的界面Activity中,使应用程序与用户进行交互响应的相关知识点,那对于应用程序中不需要与用户交互的逻辑,又要用到哪些内容呢?本文开始 ...

  3. Android -- service 服务的创建与使用,生命周期,电话监控器

    1. 为什么使用service 应用程序 : 一组组件(activity  service provider receiver)的集合. 一般情况 一个应用程序 会对应一个进程. 一般情况 关闭掉应用 ...

  4. Android服务——Service

    服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...

  5. Android服务Service

    安卓Service服务 一    Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...

  6. C# Windows Service服务的创建和调试

    前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...

  7. Android学习笔记--服务(Service)

    1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...

  8. android服务Service(上)- IntentService

    Android学习笔记(五一):服务Service(上)- IntentService 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服 ...

  9. android 后台服务定时通知

    最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知. 1.首先我们需要用到Service类. 先上代码在慢慢解释 package ...

随机推荐

  1. wifi详解(一)

    1        WLAN技术 WLAN是英文WirelessLAN的缩写,就是无线局域网的意思.无线以太网技术是一种基于无线传输的局域网技术,与有线网络技术相比,具有灵活.建网迅速.个人化等特点.将 ...

  2. [Everyday Mathematics]20150209

    设 $f$ 在区间 $I$ 上三阶可导, $f'\neq 0$, 则可定义 $f$ 的 Schwarz 导数: $$\bex S(f,x)=\frac{f'''(x)}{f'(x)}-\frac{3} ...

  3. [转载] python+Eclipse+pydev环境搭建

    转自:http://www.cnblogs.com/Bonker/p/3584707.html 编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用. ...

  4. 【LeetCode 215】Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. <转>如何测试一个杯子

    在软件测试的面试中, 经常会碰到类似的问题. 比如:如何测试一个杯子, 或者如何测试一只笔. 要求你设计20个以上的test case. 这类的面试题目,是考察面试者是否熟悉各种软件测试方法, 设计t ...

  6. C# 多线程传参 三种实例

    //using Thread to download files //1111111111111111 foreach (var str in listDownloadPdf) { //string ...

  7. bzoj 3091 城市旅行(LCT+数学分析)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3091 [思路] 膜Popoqqq大爷的题解 click here [代码]是坑... ...

  8. Activity生命周期与状态保存

    弹出系统对话框,程序仍部分可见 onPause 对话框消失时 onResume   调用一个新的Activity,老的Activity不可见时 onPause->onStop 从新的Activi ...

  9. Spring学习笔记(一) Spring基础IOC、AOP

    1.       注入类型 a)       Spring_0300_IOC_Injection_Type b)       setter(重要) c)       构造方法(可以忘记) d)     ...

  10. MapReduce Kmeans聚类算法

    最近在网上查看用MapReduce实现的Kmeans算法,例子是不错,http://blog.csdn.net/jshayzf/article/details/22739063 但注释太少了,而且参数 ...