Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过intent将对象发送到Android中的其他活动?_Android_Android Intent_Bluetooth - Fatal编程技术网

如何通过intent将对象发送到Android中的其他活动?

如何通过intent将对象发送到Android中的其他活动?,android,android-intent,bluetooth,Android,Android Intent,Bluetooth,我正在开发Android。我尝试将蓝牙对象发送到另一个活动 代码: BluetoothSocket btSocket; Intent i = new Intent(ActivityA.this, ActivityB.class); i.putExtra(EXTRA_BT_SOCKET,btSocket); startActivity(i); 但它似乎不起作用,并显示出如下错误: 无法解析方法“putExtra”(java.lang.String, android.bluetooth.

我正在开发Android。我尝试将蓝牙对象发送到另一个活动

代码

    BluetoothSocket btSocket;

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(EXTRA_BT_SOCKET,btSocket);
startActivity(i);
但它似乎不起作用,并显示出如下错误:

无法解析方法“putExtra”(java.lang.String, android.bluetooth.BluetoothSocket)

如何通过intent将Bluetooth对象发送到Android中的其他活动


提前感谢。

使用以下方法发送和检索对象:

//通过: 意图。putExtra(“MyClass”,obj)

使用助手类

public class BluetoothSocketHelper implements Serializable {

    public BluetoothSocket btSocket;

    //Constructor + getter/setters
}
从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);
然后在ActivityB中:

private BluetoothSocket btSocket;

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

您可以将其作为一个包传递给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);
在接收端(ActivityB.class)


您不能将
BluetoothSocket
实例放入
捆绑包中
或将其作为
意图
这样的“额外”传递。这将需要序列化和反序列化对象,这将生成对象的副本。你不能复制那样的套接字。您要做的只是在多个活动之间共享对象。最简单的方法是将对象的引用放在
公共静态
变量的某个地方,然后从所有需要访问的活动中使用它

嘿@Wun我知道为时已晚,但我找到了一个简单的方法,在您的第一个活动中,只需启动活动并发送您的对象:

Intent intent = new Intent(this, Second.class);
intent.putExtra("rideId", yourObject);
startActivity(intent);
在第二项活动中:

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

它显示了什么错误?@Kiya it show无法解析方法“putExtra”(java.lang.String,android.bluetooth.BluetoothSocket)。据我所知,您只能传递可以实现可包裹或可序列化接口的对象,我也像你一样被卡住了,所以我做的是使对象成为静态的,并从其他类访问它,比如:publicstaticbluetoothsocketbtsocket;当我参加其他活动时,我只做了BluetoothSocket copyOfBtSocket=ActivityA.btSocket;如果您正在尝试此方法,请确保将其强制转换为@i.putExtra(DeviceListActivity.EXTRA_地址,(可序列化)地址);我已经尝试过了,它会在I.putExtra(“MyClass”,“可序列化”btSocket)上显示“putExtra”(java.lang.String,android.bluetooth.BluetoothSocket);在检索时使用GetParsable()。OP要传递
BluetoothSocket
对象。由于此类不实现
可序列化
可打包
,因此不能以这种方式传递。此对象是与蓝牙设备的连接,因此对其进行序列化和反序列化没有任何意义。这将创建对象的副本,而您无法复制这样的套接字。OP正在尝试传递
BluetoothSocket
。此类不实现
Parcelable
Serializable
,因此不能将其放入
Bundle
中。仅通过声明
实现Serializable
就不能使
BluetoothSocket可序列化。这是一个插座,用于连接蓝牙设备。当您序列化和反序列化某个对象时,它会生成该对象的副本。不能复制这样的套接字。您不能以这种方式将套接字对象放入
捆绑包
或作为
意图
的“额外”对象。
Intent intent = new Intent(this, Second.class);
intent.putExtra("rideId", yourObject);
startActivity(intent);
String id = extras.getString("rideId");
try {
   JSONObject mJsonObject = new JSONObject(id);
} catch (JSONException e) {
   e.printStackTrace();
}