I'm developing an app with a QR scanner.
I have three activities; "Skatte" which has a button that refers to another activity called "Skat1". "Skat1" contains a QR scanner. When you scan and get a positive result, it moves on to a third acitivy called "Skat1Resultat".
My problem is that I need to close the "Skat1" activity completely (or at least just make it impossible to enter) when you have scanned a QR code, which means that I also need to make the button on "Skatte" which refers to "Skat1" unclickable.
I've read about background services, threads and intents, but I still can't figure out how to do it. I've found a code which can change text on a third activity when you scan, but I need to do it with either a button or a clickable text.
This is the code for when a QR code has been scanned and it moves from "Skat1" to "Skat1Resultat".
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
Toast toast = Toast.makeText(Skat1.this, contents, 9000);
toast.show();
startActivity(new Intent(Skat1.this, Skat1Resultat.class));
finish();
}
} else if (resultCode == RESULT_CANCELED) {
}
I though that finish(); could do the work and close the activity (I've also tried with onDestroy), but it's still possible to enter the activity when you click the button on "Skatte".
You can use a static boolean flag with value false.
Set it to true when you get "resultCode == RESULT_OK"
If the flag is false then the button is clickable
If the flag is true then the button is not clickable.
if(flag)
{
button.setClickable(false);
}
else
{
button.setClickable(true);
}
Related
I am trying to make a barcode scanning app. I am stuck at the point where I am able to scan the barcode but now I want to show the barcode image along with decoded barcode number and other details on the screen and then provide a button to proceed to next screen. How should I go about it? I am unable to understand should I call an intent to new activity or the layout view. If I call the new activity, how do I pass the barcode that's decoded and other details to new activity?
Help.
Want something like this after scanning a barcode:
you can get and use that barcode anywhere, as:
uid is a textview where i have added the result from ZXing (Zebra Crossing) library activity.:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
Log.e("test 1",String.valueOf(requestCode));
if (resultCode == RESULT_OK) {
Log.e("test 2",intent.getStringExtra("SCAN_RESULT_FORMAT"));
Log.e("test 3",intent.getStringExtra("SCAN_RESULT"));
Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT"), Toast.LENGTH_LONG).show();
uid.setText(intent.getStringExtra("SCAN_RESULT")) ;
} else if (resultCode == RESULT_CANCELED) {
Log.e("test 4",String.valueOf(requestCode));
}
}
}
Over Image you are seeing in sample image is a generated barcode not a actual picture captured from the camera.
For this you can use iText is a great Java PDF library. They also have an API for creating barcodes. You don't need to be creating a PDF to use it.
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCodeType(codeEAN.EAN13);
codeEAN.setCode("9780201615883");
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u
I have implemented some of the zebra crossing (zxing) code in an attempt to create an Android application that scans some barcodes. Pretty basic stuff really. Here are the fundamentals of my code:
public void recognizeQRClick(View view) {
printingNewQR = false;
recognizingQR = true;
text1.setText("Recognize QR button clicked");
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.initiateScan();
return;
}
#Override
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String contents = scanResult.getContents();
if(printingNewQR) {
return;
} else if(recognizingQR){
displayQRInfo(contents);
}
text1.setText(contents);
}
break;
}
}
The problem is that, while the scanning portion appears to work (when the appropriate button is clicked, the app jumps to the scanning screen, and when the barcode is centered a small golden dot appears in the center to indicate a good scan) it never finishes. The app just remains in the scanning screen, scanning the same barcode over and over. I have googled this problem extensively and found a similar issue in a couple of instances, but always their problems seem to be fixed once they use the IntentIntegrator class, which I am doing. What is wrong?
I am developing an Android application, I need to embed the Zxing scanner. The application should allow the user to scan a QR Code and then store the QR code ID of the product and parse it from an XML file. As yet, I have used the simple code:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage(getPackageName());
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
What this code does is, scans the product and bring me back to the previous screen of my app. I haven't included the entire library from Zxing as I wanted the Barcode scanner to handle it, but it seems I have to do more than I already have done.
You need to make an onActivityResult method that will get the callback once barcode scanner is done. Inside there you will handle the code string and do whatever you like with it.
/*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
Log.i("TAG",format + "\t" + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
Log.i("TAG","Canceled");
}
}
}
This example just logs the results, you'll need to expand upon it to do whatever you want with the info you get back from the scanner.
Better still, use the IntentIntegrator class supplied with the project. It wraps up all the details listed here and its documentation already tells you exactly how to integrate it into your app. It deals with things for you like getting the app installed if not already.
I have three buttons that onlongpress bring up my new IntentIntegrator scans via Barcode Scanner. I successfully have/had it scanning and using the code scanned to do something with when it is only one button.
How can I pass a value or something that when "protected void onActivityResult" is called it will know which button it came from so I can do different things with it depending on which button was long pressed.
My current setup is like this:
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
button3.setOnLongClickListener(this);}
}
public boolean onLongClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
//do stuff with the scan. But I want to do different stuff depending on which button was pressed.
Two options:
One, you can modify the IntentIntegrator source code so that it takes different REQUEST_CODES as constructor parameters (or just add a setRequestCode parameter). Then in onActivityResult, check for which requestCode was returned (each of your buttons would return a different request code). This would be the recommended approach.
Second option is the hacky one: In your code, create a member variable that tracks which was the last button pressed (check whether the view passed in onLongClick matches button1,button2, or button3), and use that information in onActivityResult to choose what to do with the results passed back from barcode scanner.