您的位置:首页 > 移动开发

Android学习系列(7)--App轮询服务器消息

2011-05-03 00:04 351 查看
这篇文章是android开发人员的必备知识。

1.轮询服务器
一般的应用,定时通知消息可以采用轮询的方法从服务器拿取消息,当然实时消息通知的话,建议采用推送服务。
其中需要注意轮询的频率设置,要在需求和性能中平衡。

2.独立进程
无论程序是否正在运行,我们都要能通知到客户,我们需要一个独立进程的后台服务。
我们需要一个独立进程的后台服务。
在AndroidManifest.xml中注册Service时,有一个android:process属性,如果这个属性以"."开头,则为此服务开启一个全局的独立进程,如果以":"开头则为此服务开启一个为此应用私有的独立进程。举个具体的例子吧,我们新建了一个 Application,创建了主进程com.cnblogs.tianxia,那么:

<!--下面会创建一个全局的com.cnblogs.tianxia.message的独立进程-->
<service android:name=".service.MessageService" android:label="消息推送" android:process=".message" />
<!--或者-->
<!--下面会创建一个应用私有的com.cnblogs.tianxia:message的独立进程-->
<service android:name=".service.MessageService" android:label="消息推送" android:process=":message" />


我们没必要建立一个全局的,本文选择第二种方案,创建一个当前应用私有的独立进程。

3.通知用户和点击查看

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 int onStartCommand(Intent intent, int flags, int startId) {
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon;
messageNotification.tickerText = "新消息";
messageNotification.defaults = Notification.DEFAULT_SOUND;
messageNotificatioManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

messageIntent = new Intent(this, MessageActivity.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 = true;
public void run() {
while(isRunning){
try {
//休息10分钟
Thread.sleep(600000);
//获取服务器消息
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();
}
}
}
}

/**
* 这里以此方法为服务器Demo,仅作示例
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage(){
return "YES!";
}
}


其中MessageActivity是点击跳转的activity,负责处理查看详细信息。
我们在其他Activity中调用一下:

boolean isMessagePush = true;//不开启就设置为false;
...
if(isMessagePush){
startService(new Intent(this, MessageService.class))
};


运行一下:



4.停止服务

stopService(new Intent(MyActivity.this,MessageService.class));
setMessagePush(false);//设置配置文件或数据库中flag为false


运行一下,停止服务后,却出乎意料的并没有停下来,怎么回事?是不是代码写错了?
代码没有错,错在我们停止了服务,却没有停止进程,退出线程。

5.退出线程
实践证明,Thread的stop()方法并不可靠。但是我们有其他的办法。
在代码面前,程序员就是上帝。
退出线程有两种方法。
第一种方法,强制退出。

//杀死该线程所在的进程,自然就退出了
System.exit(0);


第二种方法,设置isRunning为false。

//前面说到了isRunning这个标志,设置为false后,线程的执行就从while循环中跳出来了,然后自然结束掉了
messageThread.isRunning = false;


综合一下,我们在MessageService中重载onDestroy()方法如下:

@Override
public void onDestroy() {
System.exit(0);
//或者,二选一,推荐使用System.exit(0),这样进程退出的更干净
//messageThread.isRunning = false;
super.onDestroy();
}


好了,现在无论是手动停止,还是从任务管理器中强制停止Service,消息服务和消息线程都能正常的停止和退出了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: