5.Android消息推送机制简单例子
1.首先布局文件xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.riger.notificationdemo.MainActivity"> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/textView" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通知"
android:id="@+id/btnStart" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:id="@+id/btnStop" />
</LinearLayout>
</RelativeLayout>
2.主界面代码MainActivity:
public class MainActivity extends Activity implements View.OnClickListener{
private Button btnStart;
private Button btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView(){
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch(id){
case R.id.btnStart:
//启动服务
Intent intent = new Intent();
intent.setAction("ymw.MY_SERVICE");
intent.setPackage("com.example.riger.notificationdemo");
startService(intent);
break;
case R.id.btnStop:
Intent i = new Intent();
i.setAction("ymw.MY_SERVICE");
i.setPackage("com.example.riger.notificationdemo");
stopService(i);
break;
default:
break;
}
}
}
3.消息推送服务文件NotificationService
public class NotificationService extends Service{
private static final String TAG = NotificationService.class.getSimpleName();
//获取消息线程
private MessageThread messageThread = null;
//点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private NotificationManager messageNotificationManage = null;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
messageNotificationManage = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
messageIntent = new Intent(this,MainActivity.class);
messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0);
// 开启线程
messageThread = new MessageThread();
messageThread.isRunning = true;
messageThread.start();
return super.onStartCommand(intent,flags,startId);
}
/**
* 从服务器获取信息
*/
class MessageThread extends Thread{
public boolean isRunning = false;
public void run() {
try{
Log.v(TAG,"111111111");
// 间隔时间
Thread.sleep(1000);
// 获取服务器消息
String serverMessage = getServerMessage();
if (serverMessage != null && !"".equals(serverMessage)) {
Log.v(TAG,"22222222");
// 更新通知栏
Notification.Builder builder = new Notification.Builder(NotificationService.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("显示第一个通知");
builder.setContentTitle("第一个通知");
builder.setContentText("Hello World!");
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
messageNotificationManage.notify(123, notification);
Log.v(TAG,"33333333");
}
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy(){
messageThread.isRunning = false;
super.onDestroy();
}
/**
* 模拟发送信息
*/
public String getServerMessage() {
return "HELLO WORLD!";
}
}
最后记得配置下Service:
<service android:name=".NotificationService">
<intent-filter>
<action android:name="ymw.MY_SERVICE"/>
</intent-filter>
</service>
运行效果:

5.Android消息推送机制简单例子的更多相关文章
- Android (Notification)消息推送机制
从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...
- Android消息推送完美解决方案全析
推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来和大家共同探讨一种Android消息推 ...
- Android消息推送完美方案[转]
转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...
- Android消息推送完美方案
转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...
- Android消息推送的服务端
2.Android消息推送 MQTT服务器采用mosquito http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...
- (转)iOS消息推送机制的实现
原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...
- APP消息推送机制的实现(PUSH)
出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...
- ios消息推送机制原理与实现
本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
随机推荐
- RoundedBitmapDrawable生成圆角图片
Bitmap src = BitmapFactory.decodeResource(getResources(), imageId); //获取Bitmap图片 RoundedBitmapDrawab ...
- Linux make: g++: Command not found
Linux使用make命令时遇到"make: g++: Command not found",这个主要是没有安装gcc-c++.x86_64,如下所示 [root@localh ...
- [AlwaysOn Availability Groups]AlwaysOn等待类型
AlwaysOn等待类型 当排查AlwaysOn延迟,等待统计信息可以在DMV中查看累计的AlwaysOn等待类型. 查看AlwaysOn等待类型 SELECT * FROM sys.dm_os_wa ...
- oradebug/strace/pstack等分析数据库性能问题系列一
对于性能问题或者一些比较奇怪妖异的问题,有很多点可以着手去分析. 准备写一个系列关于用ash/dba_hist_active_sess_history,用oradebug,用linux命令strace ...
- jQuery标签选择器
$(function() { //alert("hello jquery"); //选择器 //id选择器 $("#bt1").click( function( ...
- .NET项目开发—浅谈面向对象的纵横向关系、多态入口,单元测试(项目小结)
阅读目录: 1.开篇介绍 2.使用委托消除函数串联调用 2.1.使用委托工厂转换两个独立层面的对象 3.多态入口(面向对象继承体系是可被扩展的) 4.多态的受保护方法的单元测试(Protected成员 ...
- 使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟
使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟 Sqoop 大数据 Hive HBase ETL 使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟 基础环境 ...
- hive建表与数据的导入导出
建表: create EXTERNAL table tabtext(IMSI string,MDN string,MEID string,NAI string,DestinationIP string ...
- Oozie coordinator 作业自定义的配置的一些方法
Oozie的coordinator有啥用? The Oozie Coordinator system allows the user to define and execute recurrent a ...
- Socket服务端和客户端(C++,CodeBlocks+GCC编译)
//main.cpp 1 #include "j_socket.h" #include <stdio.h> #include <pthread.h> ; j ...