在 Activity 中,我有一些触发消息的代码 fragment :
Message m = _handler.obtainMessage( UPDATE_PROGRESS );
Bundle bundle = new Bundle();
bundle.putInt( BUNDLE_KEY, 50 );
m.setData( bundle );
Log.d( LOG_TAG, "value " + bundle.getInt( BUNDLE_KEY, 0 ) ); // prints 50
Log.d( LOG_TAG, "sent:" + _handler.sendMessage( m ) ); // prints true
在 Activity 的另一部分,我是这样处理的:
public void handleMessage( final android.os.Message msg )
{
switch( msg.what )
{
case UPDATE_PROGRESS:
post( new Runnable()
{
@Override
public void run()
{
Bundle aBundle = msg.getData();
Log.d( LOG_TAG, "value in bundle=" +
aBundle.getInt( BUNDLE_KEY, 0 ) ); // prints 0
Log.d( LOG_TAG, "contains 'progress' : " +
aBundle.containsKey( BUNDLE_KEY ) ); // prints 'false'
}
});
break;
}
为什么 Bundle 没有保留在 Message 中?谁正在重置 Bundle 的值?
最佳答案
我知道这是一个旧线程,但由于我在使用 HandlerThread 时遇到了类似的问题,所以我想添加一些有关如何解决它的信息。
我注意到在 HandlerThread 上调用 quit 后,我的 Message 实例的内容被偶尔清除。为了解决这个问题,我使用以下方法在handleMessage回调中创建了一个带有新引用的Message对象的副本:Message msgCopy = Message.obtain(msg);
。
关于java - 为什么我的 android.os.Message 内的 Bundle 被重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1700422/