2013-04-09 16 views
0

我正在爲Android構建一個簡單的IRC客戶端。Android:如何通過AsyncTask將數據寫入scoket

我使用AsyncTask連接到Freenode/IRC-server。

我可以輕鬆獲得從IRC服務器的所有響應,

的然後我就可以顯示在響應對話的基礎。

public class IrcTask extends AsyncTask<Void, String, Void> { 
    public IrcTask(Activity activity, ProgressDialog pdialog, ScrollView sv_channel_output, List<String> join_users, BufferedWriter writer, BufferedReader reader, TextView outputView, String channel, String nick, String login) { 
     this.activity = new WeakReference<Activity>(activity);//ChannelActivity 
     this.nick = nick; 
     this.login = login; 
    } 
    @Override 
    protected Void doInBackground(Void... Sparams) { 
     // Connect directly to the IRC server. 
     try { 
      // Log on to the server. 
      this.writer.get().write("NICK " + nick + "\r\n"); 
      this.writer.get().write("USER " + login + " 8 * : Freenode for Android App IRC Bot\r\n"); 
      this.writer.get().flush(); 

      // Join to the channel. 
      this.writer.get().write("JOIN " + channel + "\r\n"); 
      this.writer.get().flush(); 

      // Read lines from the server until it tells us we have connected. 
      String line = null; 
      while ((line = this.reader.get().readLine()) != null) { 
       publishProgress(line); 
       if (line.contains("PING ")) { 
        // We must respond to PINGs to avoid being disconnected. 
        this.writer.get().write("PONG " + line.substring(5) + "\r\n"); 
        this.writer.get().flush(); 
       } 
      } 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

在我的ChannelActvity上,我有一個EditView來發送文本/數據到服務器。

我的問題是從服務器讀取數據時將一些寫入數據傳遞給AsyncTask。

(啓動doInBackground->雖然..只是運行,因此它可以將數據發回給我的ChannelActicity後)

我已經嘗試使用「SharedPreferences」來寫數據傳遞給IrcServer。

但它不工作...

就像這個...

// Read lines from the server until it tells us we have connected. 
      String line = null; 
      while ((line = this.reader.get().readLine()) != null) { 
       //gets/sets IRC-detils from 'MainActivity' 
       pref = this.activity.get().getSharedPreferences("writeToIrcTack", 0); 
       if(pref.getString("data", "").length() >0) 
       { 
        Log.d("doInBackground->IF-it-gets-data", "pref.getString=="+pref.getString("data", "")); 
        this.writer.get().write("NOTICE "+channel+" " + pref.getString("data", "") + "\r\n"); 
        this.writer.get().flush(); 
       } 
       publishProgress(line); 
+1

你寧願用搬運或廣播 – njzk2 2013-04-09 16:10:13

+0

好,我將與Hanlders嘗試.. – Voidcode 2013-04-09 23:01:54

回答

0

如果您正在使用從不同的活動/服務/意圖共享偏好/ ......你應該使用它模式MODE_MULTI_PROCESS(常數值int = 4)。如果沒有,文件被鎖定,只有一個進程可以立即寫入!

所以,當你調用多進程應用程序共享偏好做這樣的:

SharedPreferences preferences = this.getSharedPreferences("myapp",4); 

MODE_MULTI_PROCESS正在對在所有版本的Android,直到2.3,但後者必須嚴格叫!官方文檔說:

運行模式。默認操作MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE使用0或MODE_PRIVATE來控制權限。如果多個進程正在改變同一個SharedPreferences文件,那麼也可以使用MODE_MULTI_PROCESS位。 MODE_MULTI_PROCESS在定位Gingerbread(Android 2.3)及更低版本的應用中始終處於打開狀態,並且在更高版本中默認關閉。

+0

我將嘗試使用處理程序作爲njzk2評論... – Voidcode 2013-04-09 22:59:56

+0

是,處理程序與不同的服務/活動進行溝通的正確方法/ ...,但你的代碼示例呼叫也與共享首選項一起工作,你只需要像我提到的那樣調用它。 – Utisevalec 2013-04-10 06:18:51

+0

我已經實現它與處理程序現在..但上帝知道它也適用於共享偏好:) – Voidcode 2013-04-10 21:28:11