I have the following issue...I'm implementing a QR code
game in my android application...that is done by launching this intent
:
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
In other words I start the Barcode Scanner
application.
Want I want to do is launch this application and even if the user starts scanning a barcode or not the application will close automatically after two minutes.
Of course if the user doesn't close it mean time.
I tried implementing an AsyncTask
thread like this:
in onCreate()
initTask=new InitTask();
initTask.execute();
outside of onCreate()
private class InitTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void...unused){
new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
}.run();
this.cancel(true);
return null;
}
}
outside of onCreate()
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//..........
}
The problem is that the app gets opened but it doesn't close automatically after 2 minutes. Does someone know how could I achieve that?
Update
private class InitTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute()
{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1515);
}
protected Void doInBackground(Void...unused){
try
{
Thread.sleep(20000);
}
catch(Exception e)
{
System.out.println(e);
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
finishActivity(1515);
}
}
Activity Result
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1515) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
System.out.println("it is ok");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
System.out.println("it is cancel");
}
}
}