2013-07-18 46 views
17

我有什麼:的最佳方式

我使用AIDL上一個進程中運行庫。 我有一個應用程序使用這個庫,並在消息傳遞活動,我連接服務發送消息,我有一個廣播接收器來管理傳入的消息。

問題?

如果這個庫將被兩個應用程序在同一個設備上使用,那麼廣播操作將會是相同的,並且當我發送廣播時我會遇到問題。

什麼是我的疑問?

什麼是「聽」,我收到我的圖書館,並將其發送到應用程序的新收到的郵件的最佳途徑。 也許是回調?還是有更好的解決辦法?

更多信息

該庫提供了一些方法來啓動一個會話,和其他方法發送不同類型的消息(圖片,文字,地點,等...),我收到一個回調另一個使用C和C++的庫,當新消息傳入時。

如果您需要了解更多信息隨時問。

我的代碼:

IRemote.aidl

interface IRemote 
{ 
    int sendTextMessage(String to, String message); 
} 

WrapperLibrary.java

public class MyLibrary extends Service { 

    // Current context, used to sendbroadcast() from @Callbacks 
    private Context mContext = this; 

    private static MyLibrary instance = new MyLibrary(); 

    //Executor to start a new thread from the service. 
    final ExecutorService service; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     //Return the interface. 
     return mBinder; 
    } 

    /** Return the current instance */ 
    public static WrapperLibrary getInstance() { 
     return instance; 
    } 

private final IRemote.Stub mBinder = new IRemote.Stub() { 

     @Override 
     public int sendTextMessage(String to, String message) 
       throws RemoteException { 


      Log.d(TAG, "Send Text Message. "); 
      int i = -1; 
      Future<Integer> task; 
      task = service.submit(new Callable<Integer>() { 
       public Integer call() { 
        return tu.tu_message_send_text(to, message); 
       } 
      }); 
      try { 
       i = task.get(); 
      } catch (Exception e) { 
       Log.e(TAG, "Send Text Message: EXCEPTION *** " + e.getMessage()); 
      } 

      Log.d(TAG, "Send Text Message: Status Code: " + i); 

      return 0; 
     } 

} 

Callbacks.java

public class Callbacks extends JNICallback { 

    private Context mContext; 


    public Callbacks(Context context) { 
     this.mContext = context; 
    } 

    public void on_incoming_text_message(final String from, final String message) { 

     Log.d(TAG, " Incoming TEXT message from:" + from + " with message: " + message); 
     Intent i = new Intent(BroadcastActions.INCOMING_TEXT_MESSAGE); 
     i.putExtra("from", from); 
     i.putExtra("message", message); 
     mContext.sendBroadcast(i); 

    } 

} 

MainActivity.java Ø n這個活動我有一個廣播接收器,我可以用一個新的消息

public class MessageReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      Bundle extra = intent.getExtras(); 
      String incomingMessage = ""; 

      if(extra != null) { 

       incomingMessage = extra.getString("message"); 
       addNewMessage(new Message(incomingMessage, false)); 
      } 

      Toast.makeText(MessagingActivity.this, "Incoming Message", Toast.LENGTH_LONG).show(); 

     } 

    }; 
+1

你跟廣播預見哪些問題? –

+0

用於不同的應用程序相同的庫:同一個廣播動作名稱,不同的應用程序庫:我不知道到底是什麼事情發生。無論如何,你認爲這是一個好方法嗎? – Pipeline

+0

接收器背後有什麼問題..接收器是最好的選擇。所有自由和項目都是由你開發的嗎? –

回答

1

那麼,最後我會用一個Callbacks實現。 該架構將是這樣的:

應用

  • 主要活動(與LibService連接)
  • LibService(將不得不回調和廣播接收器)

  • 回調函數和接口,但未運行在ervice。

這對我來說是未來在其他項目上集成的最好方法,而且圖書館將會變得更加簡單,無需援助和服務。

我想利用接收器是一個很好的選擇,但想在其他項目上的整合,這是做到這一點的最佳方式(對我來說)。

+0

嗨,你能展示一些代碼,以便它可以幫助別人嗎? –

+0

MEHUL,當然,但我需要一些時間來清理我的代碼,寫的比較具體的迴應。 – Pipeline

+0

是的,很高興看到具體的迴應。 –

6

我會建議使用LocalBroadcastManager更新UI,或者如果它變得凌亂EventBus,但如果服務以其自身的進程運行(這不是我確定的),消息傳遞不會從一個進程傳遞到另一個進程。

所以我建議從服務中的strings.xml定義廣播事件,並使其成爲每一個應用程序不同。當然,您必須小心,因爲您還需要更新每個應用的接收器操作。

+0

服務正在自己的進程上運行,但我可以修改它。感謝你的回答。 – Pipeline

+0

關於strings.xml中,如果一個解放庫作爲的* .jar發生什麼,你不能改變的strings.xml。我在尋找一個通用的解決方案。 – Pipeline

+0

您可以將代碼部分與資源(strings.xml)並排放入庫項目中的jar中(這就是Google Play服務如何交付給我們)http://developer.android.com/tools/ projects/index.html#LibraryProjects – galex