I am making an app that le QR code and should open a page on the internet, if it is a web address, if not, show a toast if it is just a text. It opens the page of good but when it is only a text it hangs. Can someone help me?
follow the code below.
private Button btnScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btnScan);
final Activity activity = this;
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Camera Scan");
integrator.setCameraId(0);
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) {
Intent intentWeb = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getContents()));
startActivity(intentWeb);
vibrar();
//alert(result.getContents());
} else {
//alert("Scan Cancelado");
alert(result.getContents());
}
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}
private void alert(String mgs){
Toast.makeText(getApplicationContext(),mgs,Toast.LENGTH_LONG).show();
}
private void vibrar(){
Vibrator VBR = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long tempo = 300;
VBR.vibrate(tempo);
}
}