No idea what I'm doing with IntentIntegrator - java

I'm having trouble getting zxing to do anything after scanning a barcode. I call the zxing using:
IntentIntegrator.initiateScan(MainActivity.this);
And my onActivityResult looks like this
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");
if (format == "PRODUCT_CODE"){
//If the barcode scanned is of the correct type then pass the barcode into the search method to get the product details
Toast.makeText(getApplicationContext(),"You scanned " + contents, 3).show();
}
else{
//If the barcode is not of the correct type then display a notification
Toast.makeText(getApplicationContext(),"You didn't scan a product code", 3).show();
}
} else if (resultCode == RESULT_CANCELED) {
//If the scan is cancelled then display a notification
Toast.makeText(getApplicationContext(),"You cancelled the input", 3).show();
}
}
}
But whenever I exit zxing nothing is displayed. I tried using the example in the zxing wiki
but whenever I try to replace yourActivity with MainActivity or MainActivity.this I receive errors (I get told MainActivity cannot be resolved to a string and with MainActivity.this that The constructor IntentIntegrator(MainActivity) is undefined).
Basically I have no idea what I'm doing and why it's not working. Any help would be greatly appreciated.

The problem is exactly the same as in your other question. You can't compare strings with ==, but must use equals().

Related

Call function result from different activity on android studio

I found a tutorial online for creating a QR Code Scanner App for Android. It works great but the output of the scan is a toast notification as you can see in the code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK) {
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
}
}
What i would like to do is for the scanned code to launch the url inside the app itself. As I want the pages to be hosted in the app itself, what i've done was create an actvity with the webview that loads the internal html page.
I took the scanner code and changed the positive outcome to open the webview activity :
startActivity(Intent(this, PagInfoActivity::class.java))
Which works fine. Anytime a QR Code is detected the app automatically loads the desired activity. I know this is not ideal, as the QR Code link itself is not being used to open the page, but what I was trying to do was to use that scan result on the webview load. I've created QRCodes with text strings instead of URLs so that i could inject them in the loadURL of the webview like this:
WebView.loadUrl("file:///android_asset/*QRCODE string*.html");
Is it possible to call result.contents from the MainActivity?
There are multiple ways you can achieve this. First option can be Explicit Intents
For Example,
You need to pass it as an extra:
Intent i = new Intent(this, PagInfoActivity.class);
i.putExtra("result", result.contents);
startActivity(i);
Then extract it from your PagInfoActivity like this:
Intent intent = getIntent();
String result= intent.getExtras().getString("result");

onactivityresult() is never called

I am trying to send mail from my app.The problem is after successful/unsuccessful delivery of the email it doesn't return to the activity, meaning that onActivityResult() is not being called.
Here is my code:
String[] recipients = {"soham#gmail.com"};
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
// prompts email clients only
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);
try{
// the user can choose the email client
startActivityForResult(Intent.createChooser(email, "Choose an email client from..."), 1);
}catch(android.content.ActivityNotFoundException ex){
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
}else if (resultCode == RESULT_CANCELED){
}
}
}
I have checked this but not working for me. Can anyone tell me what am I doing wrong.
Edit
I got the problem.It will work fine in Activity.But it will not work on Fragment or FragmentActivity. All fragment is closing down forcefully.You can say my app is going on the background.How to solve this issue?Anybody got any idea.
If i uderstand your problem correctly;
In your activity's onActivityResult part you can find your fragment
YourFragment fragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_framelayout_id);
And use your fragment's public method:
if(fragment != null)
{
fragment.yourPublicMethod();
}
In yourPublicMethod you can do whatever you want. I hope this helps you.

How to compare voice command input by user to hard coded strings

I am currently working on an app that takes user input by voice command. The program recognizes the words using the google api. I want to be able to compare the user voice input to hard coded strings. My problem is that I do not know how to code this function. I am using if else statements to match the hard coded string, but the Toast is always the wrong one. Could anyone please guide me? Thank you for your time!
/**
* Receiving speech input
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
if(txtSpeechInput.equals("weather")) {
Toast.makeText(getApplicationContext(), "Good", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "It's a difficult question... I'm sorry", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
}
You are comparing your String to a TextView object instead of directly to the data.
Try if( result.get(0).equals("weather") ) { ... }
Or at least if( txtSpeechInput.getText().toString().equals("weather") ) { ... }

StartActivityForResult - check for existence of intent extra or use custom result code?

I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.
I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.
The first way I coded it was to check for the existence of the intent extra:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data.getExtras().containsKey("extra1")) {
extra1 = data.getStringExtra("extra1");
}
if (data.getExtras().containsKey("extra2")) {
extra2 = data.getStringExtra("extra2");
}
}
// plus rest of code for checking for RESULT_CANCELED
}
}
The other way I coded it is by using custom result codes. The result codes RESULT_OK_CATEGORY and RESULT_OK_SEARCH are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through setResult(MainActivity.RESULT_OK_CATEGORY, intent) or setResult(MainActivity.RESULT_OK_SEARCH, intent) respectively.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK_CATEGORY) {
extra1 = data.getStringExtra("extra1");
} else if (resultCode == RESULT_OK_SEARCH) {
extra2 = data.getStringExtra("extra2");
}
// plus rest of code for checking for RESULT_CANCELED
}
}
Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?
You should use resultCode because it's completely under your control. You may lose values set in Intent's extras, as revealed in this question (referenced by Ajay in the comments).
Note, if you use many custom result codes, it has been recommended to use a switch statement for code clarity.

ZXing SCAN_RESULT if statements

this is my first question on stack*overflow*!
I am using Eclipse with Android Development Tools. I have copied the code from GitHub - ZXing to scan a QR code using Barcode Scanner (many of you have seen this code before, I'm sure):
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);
}
I am able to get the Barcode Scanner started and it returns results in the onActivityResult() method.
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
contents = intent.getStringExtra("SCAN_RESULT");
format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
if(contents == "1")
{
contentsLat = contents;
}
else if(contents == "0")
{
contentsRow = contents;
}
else
{
Toast.makeText(getApplicationContext(), "Code Not Recognized\n"+contents+"\n1", Toast.LENGTH_LONG).show();
}
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Toast.makeText(getApplicationContext(), "Unsuccessful Scan", Toast.LENGTH_SHORT).show();
}
}
}
However, the else statement is always executed...unless I explicitly equate contents to 1. Below is a link to the screenshot (I don't have embedded image privileges yet) of the else body being executed and the toast clearly indicates that contents=1. What have I done wrong? Did I miss something?
Thanks everybody.
Welcome to stackoverflow!
I have seen this is a very common issue in new Java programmmers :), for String comparison we must use the equals() method.
Change your code to:
// Handle successful scan
if(contents.equals("1")){
contentsLat = contents;
}
else if(contents.equals("0")){
contentsRow = contents;
}
More info:
Java comparison with == of two strings is false?
The equals() Method

Categories

Resources