Android Messenger在注册的处理程序中未收到消息(活动通过Messenger发送IPC消息)

时间:2019-01-23 20:02:34

标签: android

我在服务中定义了Messenger,以便处理应用程序中的IPC,但是当服务由'process=":ChatsBackgroundProcess"'在单独的进程上运行时,Messenger无法接收消息。 在应用清单中声明服务。

<service
 android:name=".BackgroundService"
 android:exported="false"
 process=":ChatsBackgroundProcess"
/>
static class IncomingHandler extends Handler {
        private Context applicationContext;

        IncomingHandler(Context context) {
            applicationContext = context.getApplicationContext();
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_WE_ARE_IN_MAIN_PAGE:
                    isInMainPage = true;
                    break;

                case MSG_WE_ARE_IN_CHAT_PAGE:
                    channelId = msg.getData().getString("channelId");
                    break;
                case MSG_RESET_SERVICE:
                    isInMainPage = false;
                    channelId = "";
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Messenger service;
    private boolean bound;

    private ServiceConnection connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder remoteService) {
            // This is called when the connection with the service has been
            // established, giving us the object we can use to
            // interact with the service.  We are communicating with the
            // service using a Messenger, so here we get a client-side
            // representation of that from the raw IBinder object.
            service = new Messenger(remoteService);
            bound = true;
            makeTheServiceAware();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            service = null;
            bound = false;
        }
    };

    public void makeTheServiceAware() {
        if (!bound) return;
        // Create and send a message to the service, using a supported 'what' value
        Message msg = Message.obtain(null, BackgroundService.MSG_WE_ARE_IN_MAIN_PAGE, 0, 0);
        try {
            service.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    public void resetTheService(){
        if(!bound) return;
        // Create and send a message to the service, using a supported 'what' value
        Message msg = Message.obtain(null, BackgroundService.MSG_RESET_SERVICE, 0, 0);
        try {
            service.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, BackgroundService.class);
        startService(intent);


        bindService(new Intent(this, BackgroundService.class), connection,
                Context.BIND_AUTO_CREATE);
                      .
                      .
                      . 
   }
}

在调试模式下启动应用程序时,我没有在Service类中定义的IncommingHandler中收到消息:

public class BackgroundService extends Service {

    /**
     * Command to the service to display a message
     */
    static final int MSG_WE_ARE_IN_MAIN_PAGE = 1;
    static final int MSG_WE_ARE_IN_CHAT_PAGE = 2;
    static final int MSG_RESET_SERVICE = 3;

    private static String channelId = "";
    private static boolean isInMainPage;

    /**
     * Handler of incoming messages from clients.
     */
    static class IncomingHandler extends Handler {
                .
                .
                .


    /**
     * Target we publish for clients to send messages to IncomingHandler.
     */
    Messenger mMessenger;

    /**
     * When binding to the service, we return an interface to our messenger
     * for sending messages to the service.
     */
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
        mMessenger = new Messenger(new IncomingHandler(this));
        return mMessenger.getBinder();
    }


    .
    .
    .
    .

}

0 个答案:

没有答案