gpt4 book ai didi

android - 防止在 NFC 读取时重新打开 Activity

转载 作者:行者123 更新时间:2023-11-30 00:08:22 25 4
gpt4 key购买 nike

我有一个在 NFC 读取时打开的 Activity 。我在 Activity 应该关闭时添加了计时器。我的主要目标是用户将获得信息并且应用程序将关闭。我想做的是在当前 Activity 运行时阻止 Activity 打开(当计时器运行时,防止第二次 NFC 读取)。我为此在共享偏好中添加了 bool 值,但没有运气。 Activity 开启。你能建议我解决这个问题吗?

public class NFCTagger extends AppCompatActivity {

private static final String TAG = "DTAG";
NfcAdapter nfcAdapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
Tag myTag;
Context context;
TextView tvNFCContent;
Preference prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

context = this;
prefs = new Preference(context);

Log.d(TAG,"NFCTagger: OnCreate");

setContentView(R.layout.activity_nfctagger);
tvNFCContent = findViewById(R.id.textViewNFC);

nfcAdapter = NfcAdapter.getDefaultAdapter(this);

if (nfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
}

if (!prefs.getBool(Preference.IN_NFC_SCREEN))
{
prefs.setBool(Preference.IN_NFC_SCREEN,true);
readFromIntent(getIntent());

}

pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[]{tagDetected};
}

//Read NFC Tag

private void readFromIntent(Intent intent) {

Log.d(TAG,"Read from intent");
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
try {
buildTagViews(msgs);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

private void buildTagViews(NdefMessage[] msgs) throws ParseException {


if (msgs == null || msgs.length == 0)
return;

byte[] payload = msgs[0].getRecords()[0].getPayload();

String text = "";
try {
text = new String(payload, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

String currentTime = timeFormat.format(new Date());
tvNFCContent.setText(currentTime);


final Toast mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
mToast.show();

new CountDownTimer(6000, 1000) {

public void onTick(long millisUntilFinished) {
mToast.setText("Closing in: " + millisUntilFinished / 1000);
}

public void onFinish() {
prefs.setBool(Preference.IN_NFC_SCREEN,false);
finish();
}
}.start();

}

private void clearNFCPrefs()
{
prefs.setString(Preference.NFC_ARRIVAL,null);
prefs.setString(Preference.NFC_EXIT,null);
prefs.setString(Preference.NFC_ARRIVAL_DATE,null);
}


@Override
public void onPause() {
super.onPause();
Log.d("DTAG","NFCTagger onPause");
nfcAdapter.disableForegroundDispatch(this);
}

@Override
public void onResume() {
super.onResume();
Log.d("DTAG","NFCTagger onResume");
nfcAdapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}


@Override
protected void onNewIntent(Intent intent) {
Log.d("DTAG","NFCTagger onNewIntent");
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}

}

list :

 <activity android:name=".activities.NFCTagger">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd"/>
</intent-filter>

<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />

</activity>

最佳答案

我认为您正在寻找的是通过将此添加到 list 中的 Activity 声明来使 Activity 成为单一任务:android:launchMode="singleTask"

关于android - 防止在 NFC 读取时重新打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48588795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com