I’m working on an application that calls the ZXing scanner on button click. That works fine, after scanning the code a new activity pops up which should get the scanned code, so I could edit it put in an amount how much I want from that product, etc. But I don’t know how to get that scanned code. Here is what I have so far:
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.app.AlertDialog;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
View scanButton = findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
View editButton = findViewById(R.id.about_button);
editButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_button:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
break;
case R.id.about_button:
Intent about = new Intent(this.getApplicationContext(),About.class);
Bundle b = new Bundle();
b.putString("key","blablabla");
about.putExtras(b);
startActivityForResult(about, 0);
break;
case R.id.exit_button:
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
And the called activity named Result
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class Result extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
View finishButton =findViewById(R.id.finish_button);
finishButton.setOnClickListener(this);
View nextButton =findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
Bundle b = getIntent().getExtras();
String product = b.getString("contens").toString();
EditText et1 = (EditText) findViewById(R.id.edit_text);
et1.setText(product);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.finish_button:
finish();
break;
case R.id.next_button:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
Use their integration JAR. It provides code to handle everything you are trying to do above. You can see the source code for
IntentIntegratorhere, and here is a sample project demonstrating its use.