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