简体   繁体   English

如何通过 Intent 将对象发送到 Android 中的另一个 Activity?

[英]How to send the object via intent to another Activity in Android?

I am developing in Android.我正在Android中开发。 I tried sending a Bluetooth object to another Activity.我尝试将蓝牙对象发送到另一个活动。

Code :代码

    BluetoothSocket btSocket;

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(EXTRA_BT_SOCKET,btSocket);
startActivity(i);

But it seems to be not working and it shows an error like:但它似乎不起作用,并显示如下错误:

Cannot resolve method 'putExtra(java.lang.String, android.bluetooth.BluetoothSocket)无法解析方法 'putExtra(java.lang.String, android.bluetooth.BluetoothSocket)

How to send the Bluetooth object via intent to another Activity in Android?如何通过意图将蓝牙对象发送到 Android 中的另一个活动?

Thanks in advance.提前致谢。

You can't put a BluetoothSocket instance in a Bundle or pass it as an Intent "extra" like that.您不能将BluetoothSocket实例放入Bundle或将其作为Intent “extra” 那样传递。 This would require serializing and deserializing the object, which would make a copy of the object.这将需要序列化和反序列化对象,这将创建对象的副本。 You can't copy a socket like that.你不能像那样复制一个套接字。 What you want to do is just share the object between multiple activities.您想要做的只是在多个活动之间共享对象。 The easiest way to do that is to put a reference to the object in a public static variable somewhere and just use it from all activities that need access.最简单的方法是将对象的引用放在某个地方的public static变量中,然后从所有需要访问的活动中使用它。

Use the following to send and retrieve objects:使用以下命令发送和检索对象:

//To pass: intent.putExtra("MyClass", obj); //传递:intent.putExtra("MyClass", obj);

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

Use a helper class使用辅助类

public class BluetoothSocketHelper implements Serializable {

    public BluetoothSocket btSocket;

    //Constructor + getter/setters
}

Sending from ActivityA:从 ActivityA 发送:

BluetoothSocketHelper bluetoothSocketHelper;

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(DeviceListActivity.EXTRA_ADDRESS, address);
i.putExtra(EXTRA_BT_SOCKET, bluetoothSocketHelper);
startActivity(i);

Then in ActivityB:然后在活动B中:

private BluetoothSocket btSocket;

if(getIntent().getExtras()!=null){
    bluetoothSocketHelper = (BluetoothSocketHelper) getIntent().getSerializableExtra(EXTRA_BT_SOCKET);
    btSocket = (BluetoothSocket) bluetoothSocketHelper.getBluetoothSocket();
}

Hey @Wun I know it is too late but I found a simple way to do that, in your first activity just start the activity and send your object:嘿@Wun 我知道为时已晚,但我找到了一种简单的方法来做到这一点,在您的第一个活动中,只需启动活动并发送您的对象:

Intent intent = new Intent(this, Second.class);
intent.putExtra("rideId", yourObject);
startActivity(intent);

And in the Second Activity:在第二个活动中:

String id = extras.getString("rideId");
try {
   JSONObject mJsonObject = new JSONObject(id);
} catch (JSONException e) {
   e.printStackTrace();
}

you can pass it as a bundle through the intent你可以通过意图将它作为一个包传递

 Bundle bundle = new Bundle();
 bundle.putParcelable("yourObject key name", YOUR_OBJECT); //make YOUR_OBJECT implement parcelable
 Intent intents = new Intent(ActivityA.this, ActivityB.class);
 intents.putExtra(Constants.BUNDLE_DATA, bundle);
 startActivity(intents);

At recieving end (ActivityB.class)在接收端(ActivityB.class)

if (intent != null && intent.hasExtra(Constants.BUNDLE_DATA)) {
        Bundle bundle = intent.getBundleExtra(Constants.BUNDLE_DATA);
        if (bundle != null && bundle.containsKey(yourObject key name)) {
            Object yourObject = bundle.getParcelable(yourObject keyname);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用Intent将图片从一个活动发送到另一个活动 - How to send Images one Activity to another Activity using Intent in android 如何在Android中没有意图的情况下将数据从活动发送到另一个活动 - How to send data from activity to another activity without Intent in android 如何使用意图将具有不可序列化子级的可序列化对象从一个Android活动发送到另一个Android活动 - How to send a serializable object with nonserializable children from one Android activity to another using intent 如何通过android中的意图发送数据而不打开另一个活动? - How to send data through intent in android without opening another activity? 如何将 MutableMap 从活动发送到另一个有意图的活动 - How to send MutableMap from activity to another with intent 如何使用Xamarin在Android中通过意图将列表从一个活动传递到另一个活动 - How to pass List via intent from one activity to another activity in android using xamarin 如何通过android中的意图将字典从一个活动传递到另一个活动 - how to pass dictionary from one activity to another activity via intent in android 通过意图将 FirebaseRecyclerOption/Recycler 视图发送到另一个活动? - Send FirebaseRecyclerOption/Recycler View via Intent to Another Activity? 通过Intent将android.text.format.Time对象从一个活动传递到另一个活动 - Passing android.text.format.Time object via Intent from one activity to another 如何通过意图将数据从当前活动发送到新活动? - How to send data to new activity from current activity via intent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM