android实现通知栏消息

一、原理 
消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来;另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息。

二、步骤(代码)

注: Notification //是具体状态栏对象,设置Icon、文字、声音等。
NotificationMangager //状态栏通知管理类、负责发消息、清理消息。

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.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.widget.RemoteViews;

/**
* 消息推送

* @author Msquirrel

*/
public class MessageService extends Service {

private String TAG = "-----------";

private MessageThread messageThread = null;

// 点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;

// 通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null; // 是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
private NotificationManager messageNotificatioManager = null; // 是状态栏通知的管理类,负责发通知、清楚通知等。 
private RemoteViews contentView = null;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
// messageNotification = new
// Notification(R.drawable.icon,"新消息11",System.currentTimeMillis());//*简单消息版本里的*此版本不使用
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon;// 状态栏提示图标
messageNotification.tickerText = "嘿嘿,测试消息推送";// 状态栏提示消息

contentView = new RemoteViews(getPackageName(), R.layout.view);// 消息内容容器
contentView.setImageViewResource(R.id.image, R.drawable.icon);// 消息容器界面的图标

messageNotification.contentView = contentView;// 把消息容器和消息绑定

// messageNotification.icon = R.drawable.icon;//*简单消息版本里的*此版本不使用
// messageNotification.tickerText = "新消息11";//*简单消息版本里的*此版本不使用
// messageNotification.when=System.currentTimeMillis();
// //*简单消息版本里的*此版本不使用

// messageNotification.defaults |= Notification.DEFAULT_SOUND;//声音
// messageNotification.defaults |= Notification.DEFAULT_LIGHTS;//灯
// messageNotification.defaults |= Notification.DEFAULT_VIBRATE;//震动

// messageNotification.sound = Uri.parse("file:///sdcard/to.mp3");
messageNotification.sound = Uri.withAppendedPath(
Audio.Media.INTERNAL_CONTENT_URI, "2");// 选音乐清单的第2首歌做消息声音
// messageNotification.ledARGB = 0xff00ff00;//灯的颜色
// messageNotification.ledOnMS = 300; //亮的时间
// messageNotification.ledOffMS = 1000; //灭的时间
// messageNotification.flags |= Notification.FLAG_SHOW_LIGHTS;//显示灯

// long v[]= {0,100,200,300}; //震动频率
// messageNotification.vibrate = v;
//

messageNotification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击消息后,该消息自动退出
messageNotification.flags |= Notification.FLAG_ONGOING_EVENT;// 在上方运行消息栏中出现
// messageNotification.flags|=Notification.FLAG_NO_CLEAR;//此消息不会被清除

messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
messageIntent = new Intent(this, ShowMessage .class);// 点击消息后,要跳转的界面 ( 对应 详细消息的界面 )

// 开启线程
messageThread = new MessageThread();// 该线程每10秒,发布一条消息出来
messageThread.isRunning = true;// 设置为false后,线程跳出循环并结束对
messageThread.start();
Log.i(TAG, "startCommand");
return super.onStartCommand(intent, flags, startId);
}

/**
* 从服务器端获取消息
*/
class MessageThread extends Thread {
// 设置为false后,线程跳出循环并结束
public boolean isRunning = true;

public void run() {
while (isRunning) {
try {

String serverMessage = getServerMessage();

if (serverMessage != null && !"".equals(serverMessage)) {
// 更新通知栏
// messageNotification.setLatestEventInfo(MessageService.this,"新消息","哇~有 新消息耶!"+serverMessage,messagePendingIntent);//*简单消息版本里的*此版本不使用

contentView.setTextViewText(R.id.text, serverMessage);// 设置消息内容

messageIntent.putExtra("message", serverMessage);// 为意图添加参数
messagePendingIntent = PendingIntent.getActivity(
MessageService.this, 0, messageIntent,
PendingIntent.FLAG_CANCEL_CURRENT);// 将意图装入 延迟意图
messageNotification.contentIntent = messagePendingIntent;// 将延迟意图装入消息
messageNotificatioManager.notify(messageNotificationID,
messageNotification);// 启动Notification

Log.i(TAG, "发出消息");

// messageNotificatioManager.cancel(messageNotificationID-1);//新消息来后,消除之前的一条消息(只显示最新消息)
// 配置好下条消息的id号
messageNotificationID++;
}
// 休息10秒钟
Thread.sleep(10000);
// 获取服务器消息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

/**
* 模仿服务器发送过来的消息,仅作示例

* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage() {
Log.i(TAG, "getmessage");
return "亲, 测试成功啦~~!";

}

@Override
public void onDestroy() {
// System.exit(0);

messageThread.isRunning = false;
// 或者,二选一,推荐使用System.exit(0),这样进程退出的更干净
// messageThread.isRunning = false;
super.onDestroy();
Log.i(TAG, "destroy");
}

}

本文地址:http://www.bhcode.net/article/20140302/22603.html

android实现通知栏消息,布布扣,bubuko.com

时间: 2024-05-07 00:20:25

android实现通知栏消息的相关文章

Android手机——读取手机电话+短信+网页+图片+音乐+视频+APK+通知栏消息+换头像

Android手机--电话+短信+网页+图片+音乐+视频+APK+通知栏消息+换头像 <!--拨打电话权限--> <uses-permission android:name="android.permission.CALL_PHONE"/> <!--连接网络权限--> <uses-permission android:name="android.permission.INTERNET"/> <!--读写文件的权限

Android:通知栏的使用

非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. 本文中的代码均參照谷歌官方文档编写: http://developer.android.com/guide/topics/ui/notifiers/notifications.html 1.首先.获取系统的通知服务: NotificationManager nm = (NotificationMan

Android N 通知栏和快捷通知栏带来的改变

Android N 通知栏和快捷通知栏带来的改变 Android N 引入了一些新的API,允许应用发布具有高度可见性和交互性的通知. Android N 扩展了现有 RemoteInput 通知 API,以支持手持式设备上的内联回复. 此功能允许用户从通知栏快速进行回复,无需访问应用. 此外,Android N 还允许捆绑类似的通知并将它们显示为一则通知. 为了实现此功能,Android N 使用现有的 NotificationCompat.Builder.setGroup() 方法.用户可以

通知栏消息(Notification)初步

   Notification是用来在通知中心中显示信息的,这里讲解了其最简单的使用方式. 关于PendingIntent和Intent的区别可以参考这篇文章:http://blog.csdn.net/zeng622peng/article/details/6180190 MainActivity.java package com.kale.notification; import android.app.Activity; import android.app.Notification; im

android 本地简易消息推送

首先建一个activity,布局.xml文件的格式如图: 代码如下: 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height

Android Framework 分析---消息机制Native层

在Android的消息机制中,不仅提供了供Application 开发使用的java的消息循环.其实java的机制最终还是靠native来实现的.在native不仅提供一套消息传递和处理的机制,还提供了自定义文件描述符的I/O时间的监听机制.下面我们从具体代码中分析一下. Native层的关键类: Looper.cpp.该类中提供了pollOnce 和wake的休眠和唤醒机制.同时在构造函数中也创建 管道 并加入epoll的机制中,来监听其状态变化. Looper::Looper(bool al

深入解析Android中Handler消息机制

Android提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange).Handler消息机制可以说是Android系统中最重要部分之一,所以,本篇博客我们就来深入解析Android中Handler消息机制. Handler的简单使用 为什么系统不允许子线程更新UI 因为的UI控件不是线程安全的. 如果在多线程中并发访问可能会导致UI控件处于不可预期的状态,那为什么不对UI控件的访

Android中的消息机制

在分析Android消息机制之前.我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListener { private TextView stateText; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

浅析Android中的消息机制(转)

原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: 1 public class MainActivity extends Activity implements View.OnClickListener { 2 private TextView stateText; 3 private Button btn; 4 5 @Override 6 public void onC