So, I have 2 java classes. These are my MainActivity.java and WebActivity.java (below). What I want to to: I want to copy the string scannedurl (which I created to contain the scanned QR code) from MainActivity to WebActivity, so that it can be used as a URL somehow. I've given more details after showing both classes.
package com.example.myqrscanner;
import android.app.Activity;...
public class MainActivity extends Activity
{
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private Button button;
TextView scanText;
Button scanButton;
String scannedurl;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebActivity.class);
startActivity(intent); }});
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
// Instance barcode scanner
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanText = (TextView)findViewById(R.id.scanText);
scanButton = (Button)findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Scanare...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
}
public void onPause() {
super.onPause();
releaseCamera();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
scanText.setText("Cod QR identificat: " + sym.getData());
barcodeScanned = true;
scannedurl = sym.getData();
}
}
}
};
public String getscannedurl()
{ return scannedurl; }
// Simulate continuous auto-focusing by sending a focus request every 1000 ms
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
}
WebActivity is below:
package com.example.myqrscanner;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webcontent);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("Here I want to add the string scannedurl from MainActivty");
}
}
So I need to import that string here (webView.loadUrl(???)) after it was read by the QR scanner. I tried the getter and setter method and a few other alternatives but I got lost on the way.
Thanks in advance.
use the intent in your main activity
MainActivity.java
String status="Scanned URL";
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebActivity.class);
intent.putExtra("USERNAME",status);
startActivity(intent);
}
});
WebActivity.java
Intent intent = getIntent();
String receivedName = (String) intent.getSerializableExtra("USERNAME");
webView.loadUrl(receivedName);