当我从主Activity调用另一个Activity来获取结果时,我正试图解决一个特定的问题。
我主要担心的是名为bToggle
的DeviceListActivity中的变量,该变量正在被广播接收器修改,并且相同的变量被发送到MainActivity
,这是我无法获得的。
这是我的MainActivity
类,它调用另一个Activity来获得结果:
public class MainActivity extends Activity{
public void someMethod(){
Runnable runnableBlueDeviceList = new Runnable(){
public void run() {
Intent BlueIntent = new Intent(MainActivity.this, DeviceListActivity.class);
startActivityForResult(BlueIntent, 13);
}
};
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// if(DEBUG) Log.d(LOG_TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case 13:
// When DeviceListActivity returns with a device MAC adress to connect
// But it may be alive or dead
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
device_address = data.getExtras().getString("device_address");
device_toggle = data.getIntExtra("device_toggle", 0);
// Attempt to connect to the device
if(device_toggle==1) {
changeMenuItem();
} else {
scanFlag=0;
device_address=null;
showToast(getApplicationContext(), "BLUTOOTH DEVICE NOT ACTIVE");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
scanFlag=0;
device_address = null;
showToast(getApplicationContext(), "BLUTOOTH CONNECTION CANCELLED, " + "\n(OR) \nBLUTOOTH DEVICE NOT COMPATIBLE");
}
break;
}
}
}
DeviceListActivity
有一个变量bToggle
,由BroadcastReceiver
修改并通过意图发送到MainActivity
:
public class DeviceListActivity extends Activity {
// Debugging
private static final String TAG = "DeviceListActivity";
private static final boolean D = true;
// Return Intent extra
public static int bToggle = 0;
// The on-click listener for all devices in the ListViews
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it's costly and we're about to connect
mBtAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = " ";
if(info.length()>16)
address = info.substring(info.length() - 17);
// If the text view contains no devices found then dont proceed
if(info.contains("No devices found")) {
} else if (address.startsWith("00")) {
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra("device_address", address);
intent.putExtra("device_toggle", bToggle);
// Set result and finish this Activity
setResult(Activity.RESULT_OK , intent);
finish();
} else {
// Set result CANCELED incase the user tries to connect to other devices
setResult(Activity.RESULT_CANCELED);
finish();
}
}
};
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
scanButton.setVisibility(View.VISIBLE);
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
scanButton.setVisibility(View.VISIBLE);
}
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
bToggle = 1;
showToast(getApplicationContext(), "BLUTOOTH CONNECTION SUCCESS-FULL");
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
bToggle = 0;
showToast(getApplicationContext(), "BLUTOOTH NOT-CONNECTED");
}
}
};
所以主要的问题是我无法获得变量bToggle