jeudi 25 août 2016

Using results from QR code to trigger if statement

I'm trying to use QR codes with simple strings to trigger if statements so it will take me to the right activity in Android Studio and the scanner (adapted from javacodegeeks) works but the if statement doesn't and I can't find an example anywhere :S

Activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent; 
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class CameraActivationActivity extends AppCompatActivity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set the main content layout of the Activity
    setContentView(R.layout.activity_camera_activation);
}

        //product qr code mode
public void sendScanQR(View v) {
    try {
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        Intent intent_scan = new Intent(ACTION_SCAN);
        intent_scan.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent_scan, 0);
    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(CameraActivationActivity.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}
        //alert dialog for downloadDialog
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
             } catch (ActivityNotFoundException anfe) {
            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}

        //on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            if(contents == "foyer") {
                Intent intent_foyer = new Intent(CameraActivationActivity.this, FoyerContentActivity.class);
                startActivity(intent_foyer);
            }
        }
    }
}
}

Aucun commentaire:

Enregistrer un commentaire