当前位置: 移动技术网 > IT编程>移动开发>Android > Android顶栏定时推送消息

Android顶栏定时推送消息

2019年07月24日  | 移动技术网IT编程  | 我要评论

冷王的爬墙丑妃txt下载,中国邮政普通包裹查询,空气凤梨

在用安卓设备时,经常会应用到弹出推送消息。下面在此把我之前写的推送代码分享给大家,供大家参考,有不同见解的朋友欢迎提出,共同学习进步!

最近搜索看这个的朋友比较多。这个也只是单独的内置推送。时时推送与服务器关联 我们可以用sdk云推送来实现我们所需的需求。相关介绍内容。往下移!

首先xml

<!-- 安卓推送服务 -->
<service android:name=".messageservice"
android:enabled="true"
android:exported="true"
android:label="pushservice"
android:launchmode="singleinstance"
android:persistent="true"
android:process=":push" >
<intent-filter>
<action android:name="com.xxxx.action.my_service" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</service>
<receiver
android:name="com.tt.xxxxx.download.gtalarmreceiver"
android:permission="com.android.launcher.permission.install_shortcut"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="com.android.launcher.action.install_shortcut" />
</intent-filter>
</receiver>
<!--end add-->

里面的. :。这些符号很坑爹。不懂的可以查下字段属性说明。

推送类:

/******************************** 类 *************************************/
package com.ttad.yxcb;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.app.service;
import android.content.contentresolver;
import android.content.context;
import android.content.intent;
import android.os.ibinder;
import android.widget.toast;
import java.text.simpledateformat;
import com.tt.yingxiongchibis.download.gtdownloaderactivity;
public class messageservice extends service {
//获取消息线程
private messagethread messagethread = null;
//点击查看
private intent messageintent = null;
private pendingintent messagependingintent = null;
//通知栏消息
private int messagenotificationid = 1000;
private notification messagenotification = null;
private notificationmanager messagenotificatiomanager = null;
public ibinder onbind(intent intent) {
return null;
}
@override
public void oncreate() {
//初始化
messagenotification = new notification();
messagenotification.icon = r.drawable.app_icon_ucs;
messagenotification.tickertext = "알림";
messagenotification.defaults = notification.default_sound;
messagenotificatiomanager = (notificationmanager)getsystemservice(context.notification_service);
//点击跳转的activity
messageintent = new intent(this, gtdownloaderactivity.class);
messagependingintent = pendingintent.getactivity(this,0,messageintent,0);
//开启线程
messagethread = new messagethread();
messagethread.isrunning = true;
messagethread.start();
//toast.maketext(messageservice.this, "", toast.length_long).show();
super.oncreate();
}
/**
* 从服务器端获取消息
*
*/
class messagethread extends thread{
//运行状态,下一步骤有大用
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//休息10分钟
thread.sleep(1000);
//获取服务器消息
string servermessage = getservermessage();
if(servermessage!=null&&!"".equals(servermessage)){
//更新通知栏
messagenotification.setlatesteventinfo(messageservice.this,"알림",servermessage,messagependingintent);
messagenotificatiomanager.notify(messagenotificationid, messagenotification);
//每次通知完,通知id递增一下,避免消息覆盖掉
messagenotificationid++;
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
}
@override
public void ondestroy() {
// system.exit(0);
//或者,二选一,推荐使用system.exit(0),这样进程退出的更干净
messagethread.isrunning = false;
//super.ondestroy();
}
/**
* 这里以此方法为服务器demo,仅作示例
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public string getservermessage(){
simpledateformat sdf=new simpledateformat("hhmmss");
string date=sdf.format(new java.util.date());
string in = date;
if(date.equals("195500"))
{
string str = "잠시후 전쟁터 시작됩니다. 준비해주세요.";
return str;
}
else if(date.equals("212500"))
{
string str = "잠시후 보스전 시작됩니다. 준비해주세요.";
return str;
}
else
{
return "";
}
}
}

最后,调用方式:

//推送
intent intent = new intent();
// 设置action属性
intent.setaction("com.ttad.yxcb.action.my_service");
// 启动该service
startservice(intent);
// startservice(new intent(extextactivity.this, messageservice.class));

android消息推送知识补充:

1.引言

  所谓的消息推送就是从服务器端向移动终端发送连接,传输一定的信息。比如一些新闻客户端,每隔一段时间收到一条或者多条通知,这就是从服务器端传来的推送消息;还比如常用的一些im软件如微信、gtalk等,都具有服务器推送功能。

  推送方法如下:

  1)通过sms进行服务器端和客户端的交流通信。

  在android平台上,你可以通过拦截sms消息并且解析消息内容来了解服务器的意图,可以实现完全的实时操作。但是问题是这个方案的成本相对比较高,且依赖于运营商。

  2)循环主动定时获取

  这种方法需要客户端来做一个定时或者周期性的访问服务器端接口,以获得最新的消息。轮询的频率太慢可能导致某些消息的延迟,太快则会大量消耗网络带宽和电池。

  3)持久连接

  这个方案可以解决由轮询带来的性能问题,但是还是会消耗手机的电池。我们需要开一个服务来保持和服务器端的持久连接(苹果就和谷歌的c2dm是这种机制)。但是对于android系统,当系统可用资源较低,系统会强制关闭我们的服务或者是应用,这种情况下连接会强制中断。(apple的推送服务之所以工作的很好,是因为每一台手机仅仅保持一个与服务器之间的连接,事实上c2dm也是这么工作的。即所有的推送服务都是经由一个代理服务器完成的,这种情况下只需要和一台服务器保持持久连接即可。c2dm=cloud to device messaging)。

  相比之下第三种还是最可行的。为软件编写系统服务或开机启动功能;或者如果系统资源较低,服务被关闭后可以在ondestroy ()方法里面再重启该服务,进而实现持久连接的方式。

  c2dm内置于android的2.2系统上,无法兼容老的1.6到2.1系统;且依赖于google官方提供的c2dm服务器,由于国内的网络环境,这个服务经常不可用。

  建立在tcp协议之上的xmpp协议,不仅可提供可这种持久连接的功能,能实现服务器和客户机的双工通信,还能不依赖与系统版本和google服务器的限制,提供了比较好的解决方案。

2. xmpp协议

  xmpp全称extensible messaging and presence protocol,前身是jabber项目,是一种以xml为基础的开放式即时通讯协议。xmpp因为被google talk和网易泡泡应用而被广大网民所接触。xmpp的关键特色是,分散式的即时通讯系统,以及使用xml串流。xmpp目前被ietf国际标准组织完成了标准化工作。

  android push notification(androidpn) 是一个基于xmpp协议的java开源实现,它包含了完整的客户端和服务器端。该服务器端基本是在另外一个开源工程openfire基础上修改实现的。

  androidpn客户端需要用到一个基于java的开源xmpp协议包asmack,这个包同样也是基于openfire下的另外一个开源项目smack,不过我们不需要自己编译,可以直接把androidpn客户端里面的asmack.jar拿来使用。客户端利用asmack中提供的xmppconnection类与服务器建立持久连接,并通过该连接进行用户注册和登录认证,同样也是通过这条连接,接收服务器发送的通知。

  androidpn服务器端也是java语言实现的,基于openfire开源工程,不过它的web部分采用的是spring框架,这一点与openfire是不同的。androidpn服务器包含两个部分,一个是侦听在5222端口上的xmpp服务,负责与客户端的xmppconnection类进行通信,作用是用户注册和身份认证,并发送推送通知消息。另外一部分是web服务器,采用一个轻量级的http服务器,负责接收用户的web请求。服务器的这两方式,意义非凡:当相应的tcp端口被防火墙封闭,可以使用轮询的方式进行访问,因此又有助于通过防火墙。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网