Hello this is my first question here is the situation: I am working on a costume softkeyboard with a specific button to scan barcode, it opens the barcode scanner, after it scans, it returns to the previous screen (let's say messaging app) with the result in clip or sharedpreferences. All fine and dandy, except for the fact that the result in the EditText is from the old scan not the new one , because when the first activity get result the scan is not completed at this time (before launching the barcode scanner activity) i was trying to make handler.postDelayed in the on key function with a boolean test to wait for the scan to finish but this freeze the activity before launching the scan as result i get not responding error the on key function is :
case 2017:
//Toast.makeText(FloatingViewService.this, "NormalScan.", Toast.LENGTH_LONG).show();
test=true;
InputConnection ic = getCurrentInputConnection();
ic.deleteSurroundingText(100, 100); //pour vider le champs de saisair
Intent dialogIntent = new Intent(SimpleIME.this, NormalScan.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( dialogIntent);
ic.commitText(String.valueOf(pref.getString("code", null)),1);
NormalScan.class to scan barcode is :
public class NormalScan extends AppCompatActivity {
private ClipboardManager myClipboard;
private ClipData myClip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentIntegrator integrator = new IntentIntegrator(this);
// integrator.setOrientationLocked(false);
integrator.setPrompt("Scan a barcode or QRcode");
integrator.initiateScan();
// integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
// Use this for more customization
//integrator.setOrientationLocked(true);
// IntentIntegrator integrator = new IntentIntegrator(this);
// integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
// integrator.setPrompt("Scan a barcode");
// integrator.setCameraId(0); // Use a specific camera of the device
// integrator.setBeepEnabled(false);
// integrator.setBarcodeImageEnabled(true);
// integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
Iconify
.with(new FontAwesomeModule())
.with(new EntypoModule())
.with(new TypiconsModule())
.with(new MaterialModule())
.with(new MaterialCommunityModule())
.with(new MeteoconsModule())
.with(new WeathericonsModule())
.with(new SimpleLineIconsModule())
.with(new IoniconsModule());//// toast library
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
if (result != null) {
if (result.getContents() == null) {
// Toast.makeText(this, "Opération annulée", Toast.LENGTH_LONG).show();
//PrettyToast.showError(getApplicationContext(), "Opération annulée");
new PrettyToast.Builder(getApplicationContext())
.withMessage(" Opération annulée ✘ ") // ☑ ☒ ☓ ✓ ✓ ✕ ✖ ✗ ✘
.withDuration(Toast.LENGTH_SHORT)
.withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0))
.withTextSize(24)
.withBackgroundResource(R.drawable.background_toast_red)
.withTextColor(R.color.white)
.build()
.show();
//myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
//String a = new String();
//a=result.getContents();
// myClip = ClipData.newPlainText("text","Scan Echouer");
// myClipboard.setPrimaryClip(myClip);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
handler.postDelayed(this, 2000);
}
}, 1500);
finish();
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//editor.putString("message", "annulée");
//editor.putString("code", a);
//editor.commit();
} else {
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String a = new String();
a = result.getContents();
myClip = ClipData.newPlainText("text", a);
myClipboard.setPrimaryClip(myClip);
//PrettyToast.showSuccess(getApplicationContext(), "Le codebar sauvegarder dans la presse papier","","");
new PrettyToast.Builder(getApplicationContext())
.withMessage(" Scanned ✓ ") //☑ ✓
.withDuration(Toast.LENGTH_SHORT)
.withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0))
.withTextSize(24)
.withBackgroundResource(R.drawable.background_toast_green)
.withTextColor(R.color.white)
.build()
.show();
//Toast.makeText(getApplicationContext(), "Le codebar sauvegarder dans la presse papier",Toast.LENGTH_SHORT).show();
finish();
editor.putString("message", "codebarre");
editor.putString("code", a);
editor.putBoolean("testscan", true);
editor.commit();
// show keybord
// InputMethodManager imm = (InputMethodManager) getSystemService(SimpleIME.INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
}
else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
finish();
}
}
}