如何分离活动
How to separate activities
我的 Android 应用程序中有多个 onActivityResult
活动,但我同时遇到两个活动 运行 的问题。当我打开我的 ZXing 条形码扫描器时,它会关闭与我的蓝牙打印机的连接。这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
拆分这些活动以使它们不相互重叠的最佳方式是什么?
更新:
正如@Mahfa 所说,我需要在扫描 activity 中添加一个请求代码。这是我开始扫描的按钮代码 activity:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
我需要做什么才能向其添加 requestCode?
@Mahfa 通过他发布的链接让我走上正轨,最终我找到了解决我的问题的代码示例 http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java
这是我完成的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
case IntentIntegrator.REQUEST_CODE:
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
break;
}
我 OnClickListener
上的任何内容都不需要更改。感谢您为我指明正确的方向@Mahfa。
我的 Android 应用程序中有多个 onActivityResult
活动,但我同时遇到两个活动 运行 的问题。当我打开我的 ZXing 条形码扫描器时,它会关闭与我的蓝牙打印机的连接。这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
拆分这些活动以使它们不相互重叠的最佳方式是什么?
更新: 正如@Mahfa 所说,我需要在扫描 activity 中添加一个请求代码。这是我开始扫描的按钮代码 activity:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
我需要做什么才能向其添加 requestCode?
@Mahfa 通过他发布的链接让我走上正轨,最终我找到了解决我的问题的代码示例 http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java
这是我完成的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
case IntentIntegrator.REQUEST_CODE:
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
break;
}
我 OnClickListener
上的任何内容都不需要更改。感谢您为我指明正确的方向@Mahfa。