简体   繁体   中英

Android Java, Setting text of a field outside of the application

I'm currently writing an Android application that will allow a user to input the result of a scanned barcode into a field outside of the application.

The app will simply be a Floating Button that when pressed, will bring up a camera screen that scans and reads the barcode being pointed at, and save the result as an intent.

The user will primarily be working on Chrome when using this Floating Button.

Question: Is it possible to set the text of the current field in focus, outside of the application, as the result of the scanned barcode?

I'm using the following libraries: - com.google.zxing:core:3.3.3 - com.journeyapps:zxing-android-embedded:3.5.0@aar

Current code:

public class MainActivity extends AppCompatActivity {

FloatingActionButton fabScan;

TextView textViewScanResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Activity activity = this;

    textViewScanResult = findViewById(R.id.textViewScan);

    fabScan = findViewById(R.id.floatingActionButton_scan);

    fabScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan Barcode");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(false);
            integrator.setOrientationLocked(false);
            integrator.initiateScan();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if (result != null) {

        if (result.getContents() == null) {
            Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
        } else {
            textViewScanResult.setText(result.getContents());
        }

    } else {

        super.onActivityResult(requestCode, resultCode, data);

    }
}

Currently, i'm only setting a textView as the scanned result.

try setting browser intent with the result content.

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if (result != null) {

    if (result.getContents() == null) {
        Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
    } else {
        textViewScanResult.setText();

         //here
         Intent intent = new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://www.stackoverflow.com")); //change url to google
         intent.putExtra(result.getContents().toString());
         startActivity(intent);
    }

} else {

    super.onActivityResult(requestCode, resultCode, data);

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM