陋室铭
永远也不要停下学习的脚步(大道至简至易)

SignalA是老外写的用于实现.net端推送消息至安卓端的实现,支持版本为android 2.3或以上,由于我的版本最低是2.2,所以只有把源码下下来自己改,如果你觉得太多了可自己编译成jar引用,本人不才,对java不是很熟悉,如果此版本中有错误还请大家指出来,此图显示的是安卓2.2与4.0的版本。

 

chat_hub代码:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical"
  6.     <EditText 
  7.         android:id="@+id/chat_text" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:layout_margin="20dp" 
  11.         android:gravity="top" 
  12.         android:inputType="textMultiLine" 
  13.         android:text="" /> 
  14.  
  15. </LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/chat_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:text="" />

</LinearLayout>

ChatHub.java代码:

 

 

  1. package com.loan.zhironghuimobile; 
  2.  
  3. import org.json.JSONArray; 
  4.  
  5. import com.zsoft.SignalA.Hubs.HubConnection; 
  6. import com.zsoft.SignalA.Hubs.HubOnDataCallback; 
  7. import com.zsoft.SignalA.Hubs.IHubProxy; 
  8. import com.zsoft.SignalA.Transport.StateBase; 
  9. import com.zsoft.SignalA.Transport.Longpolling.LongPollingTransport; 
  10.  
  11. import android.app.Activity; 
  12. import android.content.OperationApplicationException; 
  13. import android.os.Bundle; 
  14. import android.widget.EditText; 
  15.  
  16. public class ChatHub extends Activity { 
  17.      
  18.     private final static String HUB_URL="http://192.168.1.200:82/signalr/hubs"; 
  19.     @Override 
  20.     protected void onCreate(Bundle savedInstanceState) { 
  21.         // TODO Auto-generated method stub  
  22.         super.onCreate(savedInstanceState); 
  23.         setContentView(R.layout.chat_hub); 
  24.         beginConnect(); 
  25.     } 
  26.     /**
  27.      * hub链接
  28.      */ 
  29.     private HubConnection conn=new HubConnection(HUB_URL, this, new LongPollingTransport()) { 
  30.         @Override 
  31.         public void OnError(Exception exception) { 
  32.         } 
  33.         @Override 
  34.         public void OnMessage(String message) { 
  35.         } 
  36.         @Override 
  37.         public void OnStateChanged(StateBase oldState, StateBase newState) { 
  38.         } 
  39.     }; 
  40.     /**
  41.      * hub代理 panderman 2013-10-25
  42.      */ 
  43.     private IHubProxy hub = null; 
  44.     /**
  45.      * 开启推送服务 panderman 2013-10-25
  46.      */ 
  47.     private void beginConnect(){ 
  48.         try { 
  49.             hub=conn.CreateHubProxy("ChatHub"); 
  50.         } catch (OperationApplicationException e) { 
  51.             e.printStackTrace(); 
  52.         } 
  53.         hub.On("addNewMessageToPage", new HubOnDataCallback()  
  54.         { 
  55.             @Override 
  56.             public void OnReceived(JSONArray args) { 
  57.                 EditText chatText=(EditText)findViewById(R.id.chat_text); 
  58.                 //chatText.setText(args.toString());  
  59.                 for(int i=0; i<args.length(); i++) 
  60.                 { 
  61.                     chatText.append(args.opt(i).toString()); 
  62.                 } 
  63.             } 
  64.         }); 
  65.         conn.Start(); 
  66.     } 
package com.loan.zhironghuimobile;

import org.json.JSONArray;

import com.zsoft.SignalA.Hubs.HubConnection;
import com.zsoft.SignalA.Hubs.HubOnDataCallback;
import com.zsoft.SignalA.Hubs.IHubProxy;
import com.zsoft.SignalA.Transport.StateBase;
import com.zsoft.SignalA.Transport.Longpolling.LongPollingTransport;

import android.app.Activity;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.widget.EditText;

public class ChatHub extends Activity {
	
	private final static String HUB_URL="http://192.168.1.200:82/signalr/hubs";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_hub);
		beginConnect();
	}
	/**
	 * hub链接
	 */
	private HubConnection conn=new HubConnection(HUB_URL, this, new LongPollingTransport()) {
        @Override
        public void OnError(Exception exception) {
        }
        @Override
        public void OnMessage(String message) {
        }
        @Override
        public void OnStateChanged(StateBase oldState, StateBase newState) {
        }
    };
    /**
     * hub代理 panderman 2013-10-25
     */
    private IHubProxy hub = null;
    /**
     * 开启推送服务 panderman 2013-10-25
     */
    private void beginConnect(){
    	try {
			hub=conn.CreateHubProxy("ChatHub");
		} catch (OperationApplicationException e) {
			e.printStackTrace();
		}
    	hub.On("addNewMessageToPage", new HubOnDataCallback() 
		{
			@Override
			public void OnReceived(JSONArray args) {
				EditText chatText=(EditText)findViewById(R.id.chat_text);
				//chatText.setText(args.toString());
				for(int i=0; i<args.length(); i++)
				{
					chatText.append(args.opt(i).toString());
				}
			}
		});
    	conn.Start();
    }
}

 

SignalR服务器端代码参照http://www.asp.net/signalr来写

 

SignalA修改过后源码下载

更多用法参照SignalA官方文档

posted on 2014-07-23 21:57  宏宇  阅读(3004)  评论(0编辑  收藏  举报