如何关闭NFC声音
How to turn off NFC sound
我的 NFC 有问题。在 Android API 16 及更高版本中,有什么方法可以关闭 NFC 声音吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traveller_read_tag);
context = this;
tvNFCContent = findViewById(R.id.nfc_contents);
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();
}
readFromIntent(getIntent());
pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, 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};
findViewById(R.id.menu).setVisibility(View.GONE);
}
private void readFromIntent(Intent 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];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
//String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
int tempCoin = Integer.parseInt( DB.getValue(context, UserValue.KEY_COIN) ) + Integer.parseInt( text );
DB.setValue(context,UserValue.KEY_COIN,String.valueOf(tempCoin));
tvNFCContent.setText("حساب شما " + text +" تومان شارژ شد.");
beep.playBeep(context, beep.TypeBeep.OK);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
},1000);
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
不,在 Android API 级别 16 上无法禁用 NFC 发现声音。只有 reader-模式 API 在 Android 中引入API 级别 19 允许您禁用通常为 NFC 发现事件生成的平台声音:。参见 NfcAdapter.enableReaderMode
and the flag NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
。
我的 NFC 有问题。在 Android API 16 及更高版本中,有什么方法可以关闭 NFC 声音吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traveller_read_tag);
context = this;
tvNFCContent = findViewById(R.id.nfc_contents);
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();
}
readFromIntent(getIntent());
pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, 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};
findViewById(R.id.menu).setVisibility(View.GONE);
}
private void readFromIntent(Intent 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];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
//String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
int tempCoin = Integer.parseInt( DB.getValue(context, UserValue.KEY_COIN) ) + Integer.parseInt( text );
DB.setValue(context,UserValue.KEY_COIN,String.valueOf(tempCoin));
tvNFCContent.setText("حساب شما " + text +" تومان شارژ شد.");
beep.playBeep(context, beep.TypeBeep.OK);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
},1000);
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
不,在 Android API 级别 16 上无法禁用 NFC 发现声音。只有 reader-模式 API 在 Android 中引入API 级别 19 允许您禁用通常为 NFC 发现事件生成的平台声音:。参见 NfcAdapter.enableReaderMode
and the flag NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
。