Search code examples
javaandroidqr-codegoogle-vision

Data transfer between the Activities of Android


I have problem I create small application with first get sms code if length it is greater than six go to activity B. Activity B show scan QR Code. Scan QR Code use library google play services vision to scan QR Code. Get value with barcode and make mix with sms code if value is correct go to Activity C if not go to activity A. My question how get value sms code and barcode value and where make ctrytography.

]

this is class with get sms code

public class SmsCodeActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }

    public final static String EXTRA_MESSAGE = "smsCode";

    private EditText smsCode;
    private Button checkSmsCodeButton;
    private TextView text_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_code);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_restart);
        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Light.ttf");
        Typeface custom_fonts2 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");

        TextView titleActivity = (TextView) findViewById(R.id.smsTitle);
        titleActivity.setTypeface(custom_fonts2);
        TextView subtitleApplication = (TextView) findViewById(R.id.stage1TextView);
        subtitleApplication.setTypeface(custom_fonts2);

        TextView subtitleText = (TextView) findViewById(R.id.subtitle_text);
        subtitleText.setTypeface(custom_fonts);

        text_info = (TextView) findViewById(text_Info);
        text_info.setTypeface(custom_fonts2);
        text_info.setVisibility(View.INVISIBLE);

        smsCode = (EditText) findViewById(R.id.editTextSmsCode);
        smsCode.setTypeface(custom_fonts);
        checkSmsCodeButton = (Button) findViewById(R.id.buttonSmsCode);
        checkSmsCodeButton.setTypeface(custom_fonts2);
        checkSmsCodeButton.setOnClickListener(this);
        setSupportActionBar(toolbar);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.buttonSmsCode) {
            String smsText = smsCode.getText().toString();

            if (smsText.isEmpty()) {
                text_info.setVisibility(View.VISIBLE);
                text_info.setText("Nie podałeś SMS Kodu");

            } else if (smsText.length() < 6) {
                text_info.setVisibility(View.VISIBLE);
                text_info.setText("Podany SMS Kod jest za krótki");
            } else if (smsText.length() == 6) {
                Intent intent = new Intent(SmsCodeActivity.this, ScanQrCodeActivity.class);
                intent.putExtra(EXTRA_MESSAGE, smsText);
                startActivity(intent);
            }
        }
    }
}

this is class with Scan OR Code

public class ScanQrCodeActivity extends AppCompatActivity {

    SurfaceView cameraPreview;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qr_code);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_restart);
        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
        TextView title_app = (TextView) findViewById(R.id.title_application_scan);
        title_app.setTypeface(custom_fonts);
        cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);
        createCameraSource();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void createCameraSource() {

        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
        final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setAutoFocusEnabled(true)
                .setRequestedPreviewSize(1600, 1024)
                .build();

        cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(ScanQrCodeActivity.this, Manifest.permission.CAMERA)) {
                    try {
                        cameraSource.start(cameraPreview.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }
        });
        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() > 0) {


                }
            }
        });

    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("ScanQrCode Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}

Solution

  • I think you need to get into Intents: https://developer.android.com/guide/components/intents-filters.html

    sending:

    String text  = "some text here";
    
    Intent i = new Intent(this, ActivityB.class);
    i.putExtra("interesting_text", text);
    startActivity(i); 
    

    receiving:

    Intent intent = getIntent();
    String text = intent.getExtras().getString("interesting_text");