xmpp/smack判断指定好有是否在线

Roster roster = LoginToolUtil.getInstance().getConnection().getRoster();
			Presence presence = roster.getPresence("user@domin");
			
			if (presence.getType() == Presence.Type.available) {//离线
				Log.v(TAG, "User is online");

//以上方法没有用

package com.sms.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.sms.activity.StartActivity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class UserIsOnlineTool extends AsyncTask<String, Object, Short>{
	private SmSEntity chatEntity = null;
	private Context context = null;
	private static final String IsOnlie_ACTION = "com.sms.action.ISONLINE";
	
	public UserIsOnlineTool(SmSEntity chatEntity, Context context)
	{
		this.chatEntity = chatEntity;
		this.context = context;
	}
	@Override
	protected void onPostExecute(Short result) {
		Log.e("m_debug", result.toString());
		Bundle bundle = new Bundle();
		bundle.putShort("isOnline", result);
		bundle.putSerializable("sms", this.chatEntity);
		
		Intent intent = new Intent(IsOnlie_ACTION);
		intent.putExtras(bundle);
		context.sendBroadcast(intent);
		
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Object... values) {
		super.onProgressUpdate(values);
	}

	/**
	 * 判断openfire用户的状态
	 *    strUrl : url格式 - http://my.openfire.com:9090/plugins/presence/status?jid=user1@my.openfire.com&type=xml
	 *    返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
	 *  说明   :必须要求 openfire加载 presence 插件,同时设置任何人都可以访问
	 */
	@Override
	protected Short doInBackground(String... arg0) {
		String url = arg0[0];
		Log.v("m_debug", url);
		 short shOnLineState = 0; //-不存在-
		 try
		    {
		       URL oUrl = new URL(url);
		       URLConnection oConn = oUrl.openConnection();
		       InputStream inputStream = oConn.getInputStream();
		    if(oConn!=null)
		    {
		    	Log.v("m_debug", "--------------------");
		        BufferedReader oIn = new BufferedReader(new InputStreamReader(inputStream));
		        Log.v("m_debug", "--------------------");
		        if(null!=oIn)
		        {
		            String strFlag = oIn.readLine();
		            Log.e("m_debug", strFlag);
		            oIn.close();
		            if(strFlag.indexOf("type=\"unavailable\"")>=0)
		            {
		                shOnLineState = 2;
		            }
		            if(strFlag.indexOf("type=\"error\"")>=0)
		            {
		                shOnLineState = 0;
		            }
		            else if(strFlag.indexOf("priority")>=0 || strFlag.indexOf("id=\"")>=0)
		            {
		                shOnLineState = 1;
		            }
		        }
		    }
		    }
		    catch(Exception e)
		    {
		    	Log.e("m_debug", "错误:" + e.toString());
		    }
		return shOnLineState;
	}
}

AsyncTask的妙用  相当于封装好的Runable

转载于:https://www.cnblogs.com/tqj-zyy/archive/2013/02/21/4559827.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值