In my application I have scan button which scan qr code. Code is like this:
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
ClearForm();
//if (!CheckCHFID())return;
pd = ProgressDialog.show(EnquireActivity.this, "", getResources().getString(R.string.GetingInsuuree));
new Thread(){
public void run(){
getInsureeInfo();
pd.dismiss();
}
}.start();
}
});
Now the problem is before I scan the code it starts finding the information which is getInsureeInfo(); How can I control that it should execute only after user scans the code successfully?
Thanks in advance.
you need to move the part that you want to happen after the scan to the onActivityResult() method.
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan.
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
}
}
}
Also I think you are going to have to use a Handler to send a message from the work thread to the main thread when it is time to hide your progress dialog. I don't think it will let you call dismiss on it from the background thread. That is just a hunch though, not tested.
put it in OnActivityResult method ,ovveride it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1:
if (resultCode == RESULT_OK) {
//put your stuff here....
break;
}
}
}
Related
I have the following code
public void changeContentOnClick(View view) {
Intent intent = new Intent(this, ChangeNodeContentActivity.class);
intent.putExtra("SELECTED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("SELECTED_NODE_CONTENT", selectedNode.getNodeContent());
startActivityForResult(intent,RESULT_OK);
Log.d(TAG, "Can I get here?");
onActivityResult(RESULT_OK, RESULT_OK, intent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String editedNodeId = data.getStringExtra("EDITED_NODE_ID");
String editedNodeContent = data.getStringExtra("EDITED_NODE_CONTENT");
Node nodeChecker = new Node(editedNodeId);
Node editedNode = new Node(editedNodeId, editedNodeContent);
Log.d(TAG, editedNode.toString());
nodes.set(nodes.indexOf(nodeChecker), editedNode);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
Somehow, after startActivityForResult method is called, everything stops. The log with message "Can I get here?" is never printed and I don't understand why not. I have followed the answer to this topic, but somehow couldn't make it work. Do I call the onActivityResult in the wrong way or place? Please help me out!
The that should send back some info from the ChangeNodeContentActivity and the one that handles the result code is the following on click listener:
public void changeContentOnClick(View view) {
Intent intent = getIntent();
selectedNode.setNodeContent(nodeContentDisplay.getText().toString());
intent.putExtra("EDITED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("EDITED_NODE_CONTENT", selectedNode.getNodeContent());
setResult(RESULT_OK, intent);
Log.d(TAG, selectedNode.toString());
finish();
}
startActivityForResult() method receives an intent and requestCode, so you should change it to:
startActivityForResult(intent, REQUEST_CODE)
And onActivityResult() method is automatically called when returning from the activity, so you should remove the direct call you made to the method, i.e. onActivityResult(RESULT_OK, RESULT_OK, intent)
I'm developing a ZXing project in Eclipse:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
Log.d("test", "button works!");
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);
b1 = (Button)findViewById(R.id.button111);
}
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");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
I found the finder pattern coordinates are in the Detector class (getTopLeft and getTopRight and getBottomLeft) but when I want to access them I'm facee with errors in the code. I don't know how I can access these variables. For example, I define a public static FinderPattern topLeft1 in the Detector class and in the processFinderPatternInfo method I add topLeft1=topLeft.
After startActivity for the result I add:
float m1=Detector.topLeft1.x;
m2=Detector.topLeft1.y;
b.setText("(x,y)="+m1+","+m2);
but when I run the program and click the button to scan: I get:
unfortunately zxing has stopped
I need 10 reputation to post an image and I can't post logcat image :-(
Can any one help me soon?
Thanks a lot
I solved my problem.I must write OnActivityResult class out of OnCreate Method! That's all!
thank you all.
The code below works for me. However, how could I embed it with other layouts from another activity?
Basically, The screen will be splitted in two parts:
Fist one will contain a header and shared buttons to use in other screens;
Second one will be the Barcode scanner or any other screen.
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
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
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
thanks
There isn no way to do this. You are invoking a third-party app and can't control how it looks.
I'm using ZXing Library as a library to my Android project. However whenever a barcode is scanned the ZXing Capture activity doesn't provide the result to my activity, it just stands there as if the code had been scanned normally via the application.
My current code is:
discount.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(DiscountActivity.this, CaptureActivity.class);
intent.setAction("com.google.xzing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.discount_dialog);
dialog.show();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
This code is wrong, and more complex than it needs to be. This is all you should be doing: http://code.google.com/p/zxing/wiki/ScanningViaIntent
So I have a very simple app I am working on. It's purpose is to collect asset data from 1 pc, and 1 or 2 monitors.
My form contains 3 edittext views, and 3 buttons (one for each asset I am collecting data for). The buttons invoke startActivityForResult for the barcode scanner, then I want to pass the result to the associated edittext view based on which button was pressed (example: press "scan" button to the right of "Asset - PC" edittext, scan and return data to it's associated edittext. Then if you press the button "scan" thats next to the "Asset - Mon1" edittext, return data to "Asset - Mon1" edittext.... and so on...)
With the code I have here, all the items work, just not as intended. Pressing any of the "scan" buttons always returns the result to the first edittext view "Asset - PC".
public class TestShit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void assetPcClick(View view) {
Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");
intent1.setPackage("com.google.zxing.client.android");
intent1.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent1, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon1Click(View view) {
Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
intent2.setPackage("com.google.zxing.client.android");
intent2.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent2, 0);
}
public void onActivityResult2(int requestCode, int resultCode, Intent intent2) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents2 = intent2.getStringExtra("SCAN_RESULT");
String format2 = intent2.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents2);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon2Click(View view) {
Intent intent3 = new Intent("com.google.zxing.client.android.SCAN");
intent3.setPackage("com.google.zxing.client.android");
intent3.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent3, 0);
}
public void onActivityResult3(int requestCode, int resultCode, Intent intent3) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents3 = intent3.getStringExtra("SCAN_RESULT");
String format3 = intent3.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents3);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
Any suggestions on how I can better manage my multiple "ActivityForResult" and "onActivityResult" 's ?
My fix, thank you for all your help!
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
In your startActivityForResult, don't use 0's on both calls... use different numbers like 0 & 1... then you can implement a switch in your onActivityResult method with the requestCode. If the requestCode = 0 then the first method has returned, if it is 1, then the second has returned. This should be the same for more calls.
public void onActivityResult(int requestCode, int resultCode, Intent intent){
switch(requestCode){
case 0: // Do your stuff here...
break;
case 1: // Do your other stuff here...
break;
case etc:
break;
}
}
The calls should be something like this then:
(For the first time)
startActivityForResult(intent1, 0);
(For the second time)
startActivityForResult(intent2, 1);
(for the third time)
startActivityForResult(intent3, 2);
(for the nth time)
startActivityForResult(intentn, n - 1);
Or, you could declare static int values to use, instead of the more unrecognisable int values.
While you startActivityForResult you send a requestcode with it,
this should be different(unique) for your every activity you are starting from your button, say button 1 starts activity request code 1, button 2 requestcode = 2, and button 3 request code =3, then for your parent activity you must have only one onActivityResult()
in this function take a switch case , scan requestcodes, requestcode = 1 will give result from first activity and request code =2 gives for activity 2 and so on...
There's nothing in Android that is ever going to recognize and call methods named onActivityResult2 or onActivityResult3. Those are just method names you made up that are going to be ignored by the system.
You need to change your code such that you pass a different request code when you call startActivityForResult(). (requestCode is the 2nd parameter to that method)
Then in onActivityResult check the requestCode to see which activity you are getting the result from, and handle accordingly.