服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 使用SignalR推送服务在Android的实现 SignalA

使用SignalR推送服务在Android的实现 SignalA

2021-03-04 11:02Android开发网 Android

SignalA是老外写的用于实现.net端推送消息至安卓端的实现,支持版本为android 2.3或以上

由于我的版本最低是2.2,所以只有把源码下下来自己改,如果你觉得太多了可自己编译成jar引用,本人不才,对java不是很熟悉,如果此版本中有错误还请大家指出来,此图显示的是安卓2.2与4.0的版本。

使用SignalR推送服务在Android的实现 SignalA

chat_hub代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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官方文档

延伸 · 阅读

精彩推荐