2017-02-14 79 views
-1

我有問題如果長度大於6,請首先獲取短消息代碼,然後創建小應用程序轉至活動B.活動B顯示掃描QR代碼。掃描QR碼使用庫谷歌播放服務願景來掃描QR碼。如果價值是正確的獲得價值與條碼,並與短碼混合如果價值是正確的去活動C,如果不去活動答:我的問題如何獲得價值短信代碼和條碼的價值,以及在哪裏做ctrytography。Android的活動之間的數據傳輸

]

這是類獲取短信代碼

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); 
      } 
     } 
    } 
} 

這是掃描或代碼

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(); 
    } 
} 
+0

請出示你到目前爲止的代碼。 – Bryan

+1

您應該使用Intents在活動之間傳遞數據。閱讀「Intent」,「startActivityForResult」,「onActivityResult」等。 – RadekJ

回答

2

我想你需要進入意圖類: https://developer.android.com/guide/components/intents-filters.html

發送:

String text = "some text here"; 

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

接收:

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