public class CheckinActivity extends Activity{
private boolean mResumed = false;
private boolean mWriteMode = false;
NfcAdapter mNfcAdapter;
PendingIntent mNfcPendingIntent;
IntentFilter[] mWriteTagFilters;
IntentFilter[] mNdefExchangeFilters;
TabelToko toko;
TabelAbsen helper;
Cursor model=null;
AlmagAdapter adapter=null;
ListView list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView viewId = (TextView)findViewById(R.id.idlogin);
DigitalClock jam = (DigitalClock)findViewById(R.id.jam);
viewId.setVisibility(View.GONE);
jam.setVisibility(View.GONE);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
toko = new TabelToko(this);
toko.insertToko();
getWaktu();
helper = new TabelAbsen(this);
list=(ListView)findViewById(R.id.listChek);
model=helper.getAll(tanggal);
startManagingCursor(model);
adapter=new AlmagAdapter(model);
list.setAdapter(adapter);
}
@Override
public void onDestroy() { super.onDestroy(); helper.close(); }
private void cekNfc (String id) throws SQLException, UnsupportedEncodingException, GeneralSecurityException{
toko.open();
Cursor c = toko.CekToko(id);
if (c.moveToFirst()){
getWaktu();
Cursor d = helper.cekAbsen(id, tanggal);
if (d.moveToFirst()==false){
//insert checkin
helper.insertAbsen(NFCSalesAttendanceActivity.idpegawai, NFCSalesAttendanceActivity.username,
id, c.getString(1), c.getString(2), tanggal, jam, "checkin");
model.requery();
Toast.makeText(this, "Checkin", Toast.LENGTH_LONG).show();
}
else{
if (d.getCount()==1){
helper.insertAbsen(NFCSalesAttendanceActivity.idpegawai, NFCSalesAttendanceActivity.username,
id, c.getString(1), c.getString(2), tanggal, jam, "checkout");
model.requery();
Toast.makeText(this, "Insert Checkout", Toast.LENGTH_LONG).show();
}
else{
d.moveToFirst(); d.moveToNext();
helper.updateAbsen(NFCSalesAttendanceActivity.idpegawai, NFCSalesAttendanceActivity.username,
id, c.getString(1), c.getString(2), tanggal, jam, "checkout",d.getString(0));
model.requery();
Toast.makeText(this, "Update Checkout", Toast.LENGTH_LONG).show();
}
}
}
else{
new AlertDialog.Builder(this).setTitle("Perhatian!").setMessage("NFC Tidak Terdaftar di Database!")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { } }).show();
}
toko.close();
}
@Override
protected void onResume() {
super.onResume();
mResumed = true;
// Sticky notes received from Android
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] messages = getNdefMessages(getIntent());
byte[] payload = messages[0].getRecords()[0].getPayload();
try {
cekNfc(new String(payload)); }
catch (SQLException e) { e.printStackTrace(); }
catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
catch (UnsupportedEncodingException e) { e.printStackTrace(); }
catch (GeneralSecurityException e) {
e.printStackTrace();
}
setIntent(new Intent()); // Consume this intent.
}
enableNdefExchangeMode();
}
private void enableNdefExchangeMode() { mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mNdefExchangeFilters, null); }
@Override
protected void onNewIntent(Intent intent) { // NDEF exchange mode
if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
NdefMessage[] msgs = getNdefMessages(intent);
byte[] payload = msgs[0].getRecords()[0].getPayload();
}
}
NdefMessage[] getNdefMessages(Intent intent) { // Parse the intent
NdefMessage[] msgs = null;
String action = intent.getAction();
//jika ada action
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; }
}
}
return msgs;
}
class AlmagAdapter extends CursorAdapter { AlmagAdapter(Cursor c) { super(CheckinActivity.this, c); }
@Override
public void bindView(View row, Context ctxt, Cursor c) {
AlmagHolder holder=(AlmagHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.checkin, parent, false);
AlmagHolder holder=new AlmagHolder(row);
row.setTag(holder);
return(row);
}
}
static class AlmagHolder {
private TextView nama=null;
private TextView alamat=null;
private TextView waktu=null;
private ImageView gambar=null;
private View row=null;
AlmagHolder(View row) {
this.row=row;
nama=(TextView)row.findViewById(R.id.namatoko);
alamat=(TextView)row.findViewById(R.id.alamat);
waktu=(TextView)row.findViewById(R.id.waktu);
gambar=(ImageView)row.findViewById(R.id.gambar);
}
void populateFrom(Cursor c, TabelAbsen helper) {
nama.setText(helper.getNamaToko(c));
alamat.setText(helper.getAlamatToko(c));
waktu.setText(helper.getWaktu(c));
if (helper.getStatusCheckin(c).equals("checkin")) { gambar.setImageResource(R.drawable.kanan); }
else if (helper.getStatusCheckin(c).equals("checkout")) { gambar.setImageResource(R.drawable.kiri); }
}
}
}
答案 0 :(得分:2)
我最近遇到了同样的问题,(每次触摸NFC标签时,似乎无法阻止您的应用程序调用onDestroy。) - &gt;编辑...
因此,您必须将所有内容保存在SharedPreferences或任何其他Stroage中。
在onCreate中,您可以重新加载您的数据,因此即使是新活动也会获得所有需要的信息。
编辑:
经过一段时间的测试,SingleTask确实对我有用。我将所有必须重新初始化的内容放入onStart()方法中。
另外我将逻辑从onResume()放到onNewIntent(),我直接捕获了Intent。
但是我仍然会保存所有内容以备将来使用:D。
答案 1 :(得分:1)
尝试将launchMode设置为singleTop
并覆盖onNewIntent()
答案 2 :(得分:0)
在清单中将Activity的启动模式设置为“singleTask”。这应该可以解决问题,因为现在不能有多个活动实例。