数週間前、私はすでにこの質問をしていますが、答えがないことを知るまで. 何が悪いのかわかりませんが、nfc タグをスキャンするたびに、同じ新しいアクティビティが常に作成されます。どうすればこれを解決できますか? ここにコード
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); }
}
}
}