首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将服务与活动(广播、回调等)通信的最佳方式

将服务与活动(广播、回调等)通信的最佳方式
EN

Stack Overflow用户
提问于 2013-07-18 19:01:41
回答 2查看 1.8K关注 0票数 17

我所拥有的:

我有一个运行在使用aidl的进程上的库。我有一个使用这个库的应用程序,在消息传递活动中,我连接到该服务来发送消息,并且我有一个广播接收器来管理传入的消息。

The problem?

如果这个库将被同一设备上的两个应用程序使用,则广播操作将是相同的,并且当我发送广播时会出现问题。

我的怀疑是什么?

如何“监听”我在库中收到的新传入消息并将其发送到应用程序的最佳方式是什么?也许是一次回调?或者还有更好的解决方案吗?

更多信息

该库提供了一些启动会话方法,以及用于发送不同类型的消息(图像、文本、位置等)的其他方法。当有新消息传入时,我收到来自使用C和C++的另一个库的回调。

如果您需要更多信息,请随时询问。

我的代码:

IRemote.aidl

代码语言:javascript
复制
interface IRemote
{
    int sendTextMessage(String to, String message); 
}

WrapperLibrary.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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在此活动中,我有一个广播接收器,我可以使用新消息更新UI

代码语言:javascript
复制
    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();

        }

    };
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-26 17:17:30

最后,我将使用一个回调实现。架构将如下所示:

应用程序

  • 主要活动(使用LibService)
  • LibService连接(将具有回调和广播接收器)

图书馆

  • 回调和接口,但不在服务中运行。

对我来说,这是未来集成其他项目的最好方法,没有aidl和服务,库将变得更加简单。

我认为使用接收器是一个非常好的选择,但考虑到与其他项目的集成,这是(对我来说)最好的方式。

票数 1
EN

Stack Overflow用户

发布于 2013-07-22 23:47:42

我打算建议使用LocalBroadcastManager,或者如果它变得混乱的EventBus,但是如果服务在它自己的进程中运行(这是我不确定的事情),消息将不会从一个进程传递到另一个进程。

因此,我建议在strings.xml中定义服务中的广播操作,并为每个应用程序设置不同的广播操作。当然,您必须小心,因为您还需要为每个应用程序更新接收器操作。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17721643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档