如果你需要我的课:
消息管理器.java
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import com.ubikinformatica.it.lib.commons.modules.log.EMaxLogger;
import java.util.ArrayList;
import java.util.List;
public class MessageManager {
public interface IOnHandleMessage{
// Message Whats
int MSG_HANDSHAKE = 0x1;
void onHandleMessage(Message msg);
}
private static final String TAG = MessageManager.class.getSimpleName();
private Messenger mMsgSender;
private Messenger mMsgReceiver;
private List<Message> mMessages;
public MessageManager(IOnHandleMessage callback, IBinder target){
mMsgReceiver = new Messenger(new MessageHandler(this, callback, MessageHandler.TYPE_ACTIVITY));
mMsgSender = new Messenger(target);
mMessages = new ArrayList<>();
}
public MessageManager(IOnHandleMessage callback){
mMsgReceiver = new Messenger(new MessageHandler(this, callback, MessageHandler.TYPE_SERVICE));
mMsgSender = null;
mMessages = new ArrayList<>();
}
/** Getter & Setter Methods **/
public Messenger getMsgSender() {
return mMsgSender;
}
public void setMsgSender(Messenger sender) {
this.mMsgSender = sender;
}
public Messenger getMsgReceiver() {
return mMsgReceiver;
}
public void setMsgReceiver(Messenger receiver) {
this.mMsgReceiver = receiver;
}
public List<Message> getLastMessages() {
return mMessages;
}
public void addMessage(Message message) {
this.mMessages.add(message);
}
/** Public Methods **/
public void sendMessage(int what, int arg1, int arg2, Bundle msgData){
if(mMsgSender != null && mMsgReceiver != null) {
try {
Message msg = Message.obtain(null, what, arg1, arg2);
msg.replyTo = mMsgReceiver;
if(msgData != null){
msg.setData(msgData);
}
mMsgSender.send(msg);
} catch (RemoteException rE) {
EMaxLogger.onException(TAG, rE);
}
}
}
public void sendHandshake(){
if(mMsgSender != null && mMsgReceiver != null){
sendMessage(IOnHandleMessage.MSG_HANDSHAKE, 0, 0, null);
}
}
}
消息处理程序.java
import android.os.Handler;
import android.os.Message;
public class MessageHandler extends Handler {
// Types
final static int TYPE_SERVICE = 0x1;
final static int TYPE_ACTIVITY = 0x2;
private MessageManager mMessageManager;
private MessageManager.IOnHandleMessage mCallback;
private int mType;
public MessageHandler(MessageManager msgManager, MessageManager.IOnHandleMessage callback, int type){
this.mMessageManager = msgManager;
this.mCallback = callback;
this.mType = type;
}
/** Override Handler Methods **/
@Override
public void handleMessage(Message msg){
this.mMessageManager.addMessage(msg);
switch(msg.what){
case MessageManager.IOnHandleMessage.MSG_HANDSHAKE:
switch(mType){
case TYPE_SERVICE:
this.mMessageManager.setMsgSender(msg.replyTo);
this.mMessageManager.sendHandshake();
break;
case TYPE_ACTIVITY:
break;
}
break;
default:
if(mCallback != null){
mCallback.onHandleMessage(msg);
}
break;
}
}
}
更多信息在我的帖子中:在这里查看我的帖子
实际上,新的 JobService 和 JobIntentService 存在一些问题,因为它们不能返回比 JobScheduler IBinder 引擎更多的 IBinder,但我现在正在尝试解决它们:D
希望这有帮助,有一个很好的编码:D再见
[重要编辑时间:02/05/2019 - 10:57]
好的问题解决了大声笑,您可以将 Messenger 放在一个包裹中,并通过意图将其发送到服务。因此,在 JobIntentService 中,“onBind”方法将返回 JobScheduler IBinder 引擎,您将在 Intent 中获取 Messenger 的 Binder!
因此,您仅使用回调在活动中初始化 MessageManager,通过“enqueueWork”启动服务,并在意图中放置 Messenger Receiver。在 JobIntentService 中,您可以通过在“onBind”和“onHandleWork”方法中使用 Messenger 的“getBinder()”方法获取 IBinder 来初始化 MessageManager。当您拥有活动的 MessengerReceiver 的 IBinder 时,您可以使用构造函数“回调 + 活页夹”来初始化 MessageManager。然后在服务中发送握手并在活动中设置他的发件人。
去做这个:
您必须更改 MessageHandler 的“handleMessage”覆盖方法:现在的方法是:
@Override
public void handleMessage(Message msg){
this.mMessageManager.addMessage(msg);
switch(msg.what){
case MessageManager.IOnHandleMessage.MSG_HANDSHAKE:
switch(mType){
case TYPE_SERVICE:
this.mMessageManager.setMsgSender(msg.replyTo);
this.mMessageManager.sendHandshake();
break;
case TYPE_ACTIVITY:
break;
}
break;
default:
if(mCallback != null){
mCallback.onHandleMessage(msg);
}
break;
}
}
您需要删除 Type 因为您不再需要它。因此,在“MessageHandler”类中的“handleMessage”方法的切换中,当您获取并握手时,您必须执行以下操作:
[...]
case MessageManager.IOnHandleMessage.MSG_HANDSHAKE:
if(this.mMessageManager.getMsgSender() == null){
this.mMessageManager.setMsgSender(msg.replyTo);
this.mMessageManager.sendHandshake();
}
[...]
这样就解决了 JobIntentService 的问题;)希望对您有所帮助!有一个很好的编码 <3