2013-07-22 90 views
2

我正在構建我的應用程序,並使用socket.io與Web服務器進行通信。我有java文件,照顧那些不是活動,並在後臺運行。因此,要啓動它們,它將在一個活動中實例化,套接字將運行。現在,web服務器觸發我在回調中處理的消息並將數據傳遞給活動。現在我正在尋找將數據從正在運行的非活動類傳遞到當前正在運行的活動的最佳方式。如何將數據從非活動類傳遞給android中的活動?

我之前所做的是使用非活動類創建的意圖,該類將數據發送到當前正在運行的活動。爲了避免活動繼續自行重啓,必須將活動改爲單實例或單線。 問題是,形式我的主要行動我不能使用單頂發射模式,因爲我需要在某些情況下刷新本身的行動。

所以我不能正確地處理主要活動的套接字,因爲我只能接收數據,如果我將主要活動更改爲不適合該應用程序的singletop。我首先嚐試在非活動類中調用的活力命名函數,但看起來這是非法的,所以我很困難。

@Override 
    public void on(String event, IOAcknowledge ack, Object... data) { 

     System.out.println("Server may have triggered event '" + event + "'"); 

     if (event.equals("message")) { 
     System.out.println(data[2]); 

     Intent newIntent = new Intent(Chat.mContent, Chat.class); 
     Bundle extras=new Bundle(); 
     extras.putString("name", (String)data[1]); 
     extras.putString("MSG", (String)data[2]); 
     newIntent.putExtras(extras); 


     Chat.mContent.startActivity(newIntent); 

     } 
     if (event.equals("ready")) { 
      System.out.println("Received the ready callback"); 
      try{ 
      Intent newIntent = new Intent(TabExercise.mContext, TabExercise.class); 
      newIntent.putExtra("ready", event); 
      TabExercise.mContext.startActivity(newIntent); 
      }catch(Exception e){ 
       System.out.println("Caught an error"); 
      } 


     } 
    } 

這是發送數據的非活動類中的方法。

@Override 
protected void onNewIntent(Intent intent) { 
super.onNewIntent(intent); 
Bundle extras=intent.getExtras(); 

if (intent.hasExtra("ready")) { 
    // Retrieve the message and call getMsg(String) 
    connect.starter(SaveSharedPreference.getName(TabExercise.this), SaveSharedPreference.getUserId(TabExercise.this)); 

} 

}

這是哪裏的意圖被捕獲在MainActivity。不理想。

+0

共享PERF可能工作 – KOTIOS

+0

嗯,我不明白如何,將工作,因爲它需要實時工作。 –

回答

2

我認爲您可能會發現有用的是使用Singletons或可能是非UI Fragment。如果你沒有使用片段已經爲你的用戶界面,你應該肯定look into it,因爲它是現代android應用程序的法律標準。更重要的是,Android支持庫將片段一直提供給Android 1.6,這是後向兼容的理想選擇。

您可以在線程之間傳遞使用Handlers的數據和消息。這些基本上提供了一條管道,您可以將其中的post消息發送給handleMessage。你可能會在ApplicationonCreate開始你的網絡的東西,也許存儲線程爲singleton。然後,您會將對您的用戶界面Handler的引用傳遞給此線程,然後通過發佈Runnables直接與您的用戶界面進行通信。

對於這一個有趣的答案是「好」可以在這裏找到:

Non-UI Fragment vs Singleton

+0

嗯好,謝謝你的答案。我會研究它。 –

+0

爲了幫助您,如果您希望在整個應用程序的整個過程中(包括活動之間)在後臺進行通信/監聽,那麼您應該考慮擴展'Application'類並在那裏初始化您的'singletons'然後。重寫的方法是'onCreate',它只在應用程序第一次啓動時被調用一次。但是,如果該進程被android終止(例如由於內存緊張),當用戶返回時它將被再次調用。 – Dororo

+0

非常感謝。無論啓動模式如何,我都能完成您的概念工作,並大大簡化了我的代碼。 –

1

據我瞭解的問題,你開始你的活動,從那裏你開始你的CommunicationHandler實例?

然後您將讓您的活動實現任何類型的CallBack接口,使用CallBack(表示:活動)實例化您的Handler並調用CallBacks調用方法。

看到this question and it's answer關於如何使用回調

對不起一些更多的細節,如果我的理解是錯誤的:-)

編輯:

你MainActivity(一開始你SocketConnector和接收消息)應該提供一個回調接口的實現。該接口可以是這個樣子:

public interface IMessageReceiver { 
    public void onMessageReceived(Message m); 
} 

與Message類看起來像:

public class Message { 
    String name; 
    String msg; 
    //add getter/setter and constructor to fit your needs 
} 

那麼你的主要活動應該是這樣的:

public class MainActivity extends Activity implements IMessageReceiver { 
    //... main activity code here. 
    SocketConnector mySC = new SocketConnector(this); 
    //... more main activity code here. 
    public void onMessageReceived(Message m) { 
    //do something with the message, e.g. display to edit texts, lists etc. 
    } 
} 

,並在您SocketConnector你會將IMessageReceiver定義爲最終成員並調用receiver.onMessageReceived(m) - 應該像

IMessageReceiver receiver 

public SocketConnector(IMessageReceiver receiver) { 
    this.receiver = receiver; 
} 

@Override 
public void on(String event, IOAcknowledge ack, Object... data) { 
    if (event.equals("message")) { 
     System.out.println(data[2]); 

     //Intent newIntent = new Intent(Chat.mContent, Chat.class); 
     //Bundle extras=new Bundle(); 
     //extras.putString("name", (String)data[1]); 
     //extras.putString("MSG", (String)data[2]); 
     //newIntent.putExtras(extras); 


     //Chat.mContent.startActivity(newIntent); 
     Message m = new Message(); 
     m.name = (String)data[0]; 
     m.msg = (String)data[1]; 
     this.receiver.onMessageReceived(m); 
    } 
} 
+0

好吧,不是真的。活動確實會啓動通信處理程序,該處理程序依次啓動socketconnector和回調接口。回調已經被處理和接收,現在的問題是如何從回調中獲取值到正在運行的活動。 –

+0

你有沒有聲明你的接口的參數調用方法?讓我爲此添加一些代碼 –

+0

是的。 public void on(String event,JSONObject data);這就是如何在界面中設置回調。 –

相關問題