我有一个使用zxing扫描仪的基本Android应用程序和一个带3个按钮的活动。扫描器向活动发送代码,用户可以选择进入,退出或取消操作。这很好用。我想在此过程中添加自动超时,以便在用户未在特定时间段内单击按钮时,系统将确定它们是否处于打开状态(必须按下取消按钮)。我有所有这些的逻辑,但我不知道如何添加超时逻辑。有初始布局,活动Java代码,SQLite数据库对象和处理程序,zxing集成和ASync帖子。我在下面包含了主要的java类,其中包括onClick事件以及zxing和SQLite之间的主要集成(省略了这些)。
关于如何实现基本超时的任何想法?如果没有按下按钮,我想从主java类调用一个函数,这个函数将确定(根据最后的选择)它们是否进入。
感谢您提供的任何帮助或建议。
package com.neonet.neonetjobcardscanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.channels.FileChannel;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.neonet.neonetjobcardscanner.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@SuppressWarnings("unused")
// remove before launch
public class ClockInOut extends Activity implements OnClickListener, OnInitListener {
private Button btnClockIn;
private Button btnClockOut;
private Button btnCancel;
private String employee;
private String operation;
private TextToSpeech tts;
private IntentIntegrator scanIntegrator;
private neonetpost post;
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale("en", "EN"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock_in_out);
btnClockIn = (Button) findViewById(R.id.btnClockIn);
btnClockOut = (Button) findViewById(R.id.btnClockOut);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnClockIn.setOnClickListener(this);
btnClockOut.setOnClickListener(this);
btnCancel.setOnClickListener(this);
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan(); // optional scan type can be passed here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.clock_in_out, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
post = new neonetpost();
String result = null;
String in = null;
String confirmation = null;
String now = df.format(new Date());
try {
SQLiteHandler db = new SQLiteHandler(getApplicationContext());
if (v.getId() == R.id.btnClockIn) {
in = "true";
if (operation != null)
confirmation = getString(R.string.ttsJobIn);
else
confirmation = getString(R.string.ttsClockIn);
Log.d("Insert: ", "Inserting...");
db.addTimeclockEntry(new TimeclockEntry(Integer.valueOf(employee), now, "IN"));
} else if (v.getId() == R.id.btnClockOut) {
in = "false";
if (operation != null)
confirmation = getString(R.string.ttsJobOut);
else
confirmation = getString(R.string.ttsClockOut);
db.addTimeclockEntry(new TimeclockEntry(Integer.valueOf(employee), now, "OUT"));
} else if (v.getId() == R.id.btnCancel) {
}
db.close();
db.exportDB(getApplicationContext());
if (in != null) {
post.execute(employee, operation, in);
while (result == null) {
result = post.result();
}
tts.speak(confirmation + " " + (operation!=null?result:""), TextToSpeech.QUEUE_FLUSH, null);
}
employee = operation = null;
} catch (Exception e) {
if (e != null) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
Log.e("JobCardScanner",
"ClockInOut.java onclick(): " + String.format(e.toString() + "%n" + errors.toString()));
}
}
if (post.hasError())
Log.e("JobCardScanner", "ClockInOut.java onclick(): " + (result == null ? "NULL" : result));
else
Log.i("JobCardScanner", "ClockInOut.java onClick(): " + (result == null ? "NULL" : result));
scanIntegrator.initiateScan(); // optional scan type can be passed here
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
try {
// retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
// we have a result
final String scanContent = scanningResult.getContents();
// String scanFormat = scanningResult.getFormatName();
if (scanContent.indexOf("E") > -1) {
employee = scanContent.substring(2);
if (operation == null) {
btnClockIn.setText(R.string.btnClockIn);
btnClockOut.setText(R.string.btnClockOut);
} else {
btnClockIn.setText(R.string.btnOperationStart);
btnClockOut.setText(R.string.btnOperationEnd);
}
} else if (scanContent.indexOf("OP:") > -1) {
operation = scanContent.substring(3) + getString(R.string.operation_separator);
tts.speak(getString(R.string.ttsJobScan), TextToSpeech.QUEUE_FLUSH, null);
scanIntegrator.initiateScan();
}
Log.i("JobCardScanner",
"ClockInOut.java onActivityResult(): Received " + scanContent);
} else {
Log.i("JobCardScanner", "ClockInOut.java onActivityResult(): No scan data received");
}
} catch (Exception e) {
if (e != null) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
Log.e("JobCardScanner", "ClockInOut.java onActivityResult(): " + String.format(e.toString() + "%n" + errors.toString()));
}
}
}
@Override
public void onInit(int arg0) {
scanIntegrator.initiateScan();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.neonet.neonetjobcardscanner.ClockInOut" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtClockInOut"
android:textAlignment="center" />
<Button
android:id="@+id/btnClockIn"
style="@style/AppBaseTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:minHeight="@dimen/button_min_height"
android:minWidth="@dimen/button_min_width"
android:text="@string/btnClockIn"
android:textAlignment="center"
android:textSize="@dimen/button_text_size"
android:textStyle="bold" />
<Button
android:id="@+id/btnClockOut"
style="@style/AppBaseTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnClockIn"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:minHeight="@dimen/button_min_height"
android:minWidth="@dimen/button_min_width"
android:text="@string/btnClockOut"
android:textAlignment="center"
android:textSize="@dimen/button_text_size"
android:textStyle="bold" />
<Button
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnClockOut"
android:layout_centerHorizontal="true"
android:minHeight="@dimen/button_min_height"
android:minWidth="@dimen/button_min_width"
android:textAlignment="center"
android:textSize="@dimen/button_text_size"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:text="@string/btnCancel" />
</RelativeLayout>
答案 0 :(得分:0)