Parcelable自定义处理程序,消息从未收到

编程入门 行业动态 更新时间:2024-10-22 13:50:18
本文介绍了Parcelable自定义处理程序,消息从未收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要parcelable Handler对象由包从一个活动向它发送到服务,以获得从这项服务中的一些信息。

I want to parcelable a Handler object to send it by a Bundle from one Activity to a service in order to get some info from this service.

现在,测试它是一个简单的消息。下面是活动中的code:

Right now, to test it it's a simple message. Here's the code in the Activity:

private MyHandler mHandlerSharing = new MyHandler() { public void handleMessage(Message msg) { // this line in the Activity is never reached when debugging String data = msg.getData().getString("data"); Toast.makeText(mContext, data, Toast.LENGTH_SHORT).show(); } }; // in some function mSecureSharing.putExtra(Constants.HANDLER, (Parcelable) mHandlerSharing);

然后,在服务onStartCommandMetehod我做到以下几点:

Then, in the Service onStartCommandMetehod I do the following:

MyHandler myHandler = (MyHandler) intent.getExtras().getParcelable(Constants.HANDLER); Message msgObj = myHandler.obtainMessage(); Bundle b = new Bundle(); b.putString("data", "SecureSharing running"); msgObj.setData(b); myHandler.sendMessage(msgObj);

该MyHandler的类是以下内容:

The class MyHandler is the following:

import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; public class MyHandler extends Handler implements Parcelable{ private int mData; public MyHandler(){ super(); } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyHandler> CREATOR = new Parcelable.Creator<MyHandler>() { public MyHandler createFromParcel(Parcel in) { return new MyHandler(in); } public MyHandler[] newArray(int size) { return new MyHandler[size]; } }; private MyHandler(Parcel in) { mData = in.readInt(); }

}

该服务接收自定义处理程序,但从来没有,但并调用从类处理器的sendMessage方法,但从来没有活动收到消息...

The service receives the custom handler but never but and call sendMessage method from the class Handler, but the Activity never receives the message...

有关类MyHandler的,我基本上是用code从 Android开发者网站并添加处理器继承和构造函数。

For MyHandler class, I basically used the code from the android developer site and add the Handler inheritance as well as a constructor.

什么可能是错的?

在此先感谢!

推荐答案

我想你想你的活动与服务进行通信,这些可以成为你的选择:

I assume you want your activity to communicate with service these can be your choices:

  • 使用ResultReceiver一个例子可以找到的这里
  • 使用LocalBroadcastReceiver例子可以发现,这里
  • 您可以使用活页夹绑定到你的活动实例服务可以在这里找到
  • 如果你想使用RPC,那么你应该去信使的例子可以找到的这里和这里
  • 对于RPC您还可以使用AIDL文件的例子可以找到的此处,当然在开发者网站的
  • Use ResultReceiver an example could be found here
  • Use LocalBroadcastReceiver example could be found here
  • You can use Binder to bind your service to activity example could be found here
  • If you want to use RPC then you should go with Messanger an example could be found here and here
  • For RPC you can also use AIDL files example could be find here and of course on developer site
  • 更多推荐

    Parcelable自定义处理程序,消息从未收到

    本文发布于:2023-10-13 00:03:58,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1486248.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   消息   程序   Parcelable

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!