如何分开活动(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所述,我需要在扫描活动中添加requestCode。 这是我启动扫描活动按钮的代码:

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?

I have several onActivityResult activities in my Android app and I am having an issue with two activities running at one time. When I open up my ZXing barcode scanner it will close the connection to my Bluetooth printer. Here is my code:

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(); } }

What would be the best way to split up these activities so that they do not overlap each other?

UPDATE: As @Mahfa stated I need to add a requestCode to the scanning activity. This is my code for the button that starts the scanning 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);

What do I need to do to add a requestCode to this?

最满意答案

@Mahfa通过他发布的链接让我走上了正确的轨道,最终我找到了这个代码示例,解决了我的问题http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-更新主/ 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上更改任何OnClickListener 。 谢谢你指点我正确的方向@Mahfa。

@Mahfa put me on the right track with the links that he posted and eventually I found this code example that cleared up my issue http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java

Here is my completed code:

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; }

I did not have to change anything on my OnClickListener. Thanks for pointing me in the right direction @Mahfa.

如何分开活动(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所述,我需要在扫描活动中添加requestCode。 这是我启动扫描活动按钮的代码:

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?

I have several onActivityResult activities in my Android app and I am having an issue with two activities running at one time. When I open up my ZXing barcode scanner it will close the connection to my Bluetooth printer. Here is my code:

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(); } }

What would be the best way to split up these activities so that they do not overlap each other?

UPDATE: As @Mahfa stated I need to add a requestCode to the scanning activity. This is my code for the button that starts the scanning 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);

What do I need to do to add a requestCode to this?

最满意答案

@Mahfa通过他发布的链接让我走上了正确的轨道,最终我找到了这个代码示例,解决了我的问题http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-更新主/ 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上更改任何OnClickListener 。 谢谢你指点我正确的方向@Mahfa。

@Mahfa put me on the right track with the links that he posted and eventually I found this code example that cleared up my issue http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java

Here is my completed code:

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; }

I did not have to change anything on my OnClickListener. Thanks for pointing me in the right direction @Mahfa.