Android 在另一个活动中调用活动并获取方法
我正在开发一个android应用程序,我有一个问题,希望你能帮我解决。 所以我有一个名为main的活动1和名为myown的活动2。在myown中,我有一个公共类,我想在MainActivity中调用它。以下是两项活动的代码: MainActivity.javaAndroid 在另一个活动中调用活动并获取方法,android,android-activity,Android,Android Activity,我正在开发一个android应用程序,我有一个问题,希望你能帮我解决。 所以我有一个名为main的活动1和名为myown的活动2。在myown中,我有一个公共类,我想在MainActivity中调用它。以下是两项活动的代码: MainActivity.java 将此添加到您的活动中 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterV
将此添加到您的活动中
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//add this
Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
MainActivity.this.startActivity(launchActivity2);
//
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
for (BluetoothDevice device : pairedDevices) {
if ("bluetooth".equals(device.getName())) {
Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
MainActivity.this.startActivity(launchActivity2);
break;
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(true)) {
Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_SHORT).show();
//do nothing
}else {
Toast.makeText(getBaseContext(), "Unable to connect Light",Toast.LENGTH_SHORT).show();
}
view.setEnabled(false);
}
}
});
发送项目点击意向到另一个活动你能告诉我怎么做吗;方法你想在这里做lv.setOnItemClickListener吗?是的,很抱歉我就是这个意思,但是当我从myown活动调用a方法时,它说无法解析symbolconnectBT=new ConnectToBluetoothdiscoveredDevice;新ThreadconnectBT.start;例如,如果我在添加的代码下编写此代码
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.io.IOException;
import java.util.UUID;
public class myown {
public class myOwnBroadcastReceiver extends BroadcastReceiver {
ConnectToBluetooth connectBT;
public BluetoothSocket scSocket;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
}
class ConnectToBluetooth implements Runnable {
private BluetoothDevice btShield;
private BluetoothSocket mySocket = null;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public ConnectToBluetooth(BluetoothDevice bluetoothShield) {
btShield = bluetoothShield;
try {
mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
} catch (IOException createSocketException) {
}
}
@Override
public void run() {
try {
mySocket.connect();
scSocket = mySocket;
} catch (IOException connectException) {
try {
mySocket.close(); //try to close the socket
} catch (IOException closeException) {
}
return;
}
}
// Will allow you to get the socket from this class
public BluetoothSocket getSocket() {
return mySocket;
}
/* Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mySocket.close();
} catch (IOException e) {
}
}
}
}
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//add this
Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
MainActivity.this.startActivity(launchActivity2);
//
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
for (BluetoothDevice device : pairedDevices) {
if ("bluetooth".equals(device.getName())) {
Intent launchActivity2 = new Intent(MainActivity.this, myown.class);
MainActivity.this.startActivity(launchActivity2);
break;
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(true)) {
Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_SHORT).show();
//do nothing
}else {
Toast.makeText(getBaseContext(), "Unable to connect Light",Toast.LENGTH_SHORT).show();
}
view.setEnabled(false);
}
}
});