Pobieranie danych pomiędzy aktywnościami

0

Witam,

Mam pewien problem. Otóż pisze aplikacje na Androida, która jako pierwsza podaje wartość sms następnie po wpisaniu tej wartości przechodzimy do skanowania QR kodu. Pobieramy wartość z danego Qr Kodu .(Skanowanie robie za pomoca bibilteki google vison API) Następnie w wyniku wartości z SMS kodu oraz z wartości QR kodu robię własne szyfrowanie. (Szyfrowanie działa zrobiłam na Javie statycznie , wszytko działa poprawnie.) Pytanie , gdzie napisać tę metodę. nie mam zielonego pojęcia jak to zrobić.

Klasa Sms Kod

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 static int REQUEST_CODE = 1;
    private EditText smsCode;
    private Button checkSmsCodeButton;
    private TextView text_info;
    // private


    @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 smsKod = smsCode.getText().toString();

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

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

 

Klasa Scan QR 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) {
                    Intent intent = new Intent();
                    intent.putExtra("barcode", barcodes.valueAt(0));
                    setResult(CommonStatusCodes.SUCCESS, intent);
                    finish();

                }
            }
        });

    }


    /**
     * 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();
    }
}


0

W ScanQrCodeActivity w metodzie onCreate musisz odebrać kod sms, który przekazałaś za pomocą Intent.putExtra przy uruchamianiu aktywności, a z tego co widzę w ogóle tego nie robisz.

Musisz w onCreate dać String sms = getIntent().getStringExtra("smsCode");

Teraz gdy już odczytasz QR code możesz wywołać swoją metodę.

1 użytkowników online, w tym zalogowanych: 0, gości: 1