当前应用拦截NFC响应,不弹出选择框教程
生活随笔
收集整理的這篇文章主要介紹了
当前应用拦截NFC响应,不弹出选择框教程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從上一篇的NFC支持所類型的卡讀取之后,下面要解決的就是NFC的攔截響應,如果這一步沒有做,當系統內有多個支持NFC的應用的時候,就會在nfc刷卡的時候彈出多個應用選擇,我們需要的場景是,當前應用需要用NFC才去刷卡,然后本應用攔截intent分發(Using the Foreground Dispatch System).
具體的方案:
1.創建PendingIntent來分發要響應的Activity
mPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, NFCActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
2.當刷卡的時候,使用intent過濾器來過濾出你要攔截的Intent
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);try {ndef.addDataType("*/*");} catch (IntentFilter.MalformedMimeTypeException e) {throw new RuntimeException("fail", e);}intentFiltersArray = new IntentFilter[]{ndef,};
3.設置你要處理的tag technologies到String數組中
techListsArray = new String[][]{new String[]{NfcA.class.getName()}};
4.在onResume和onPause中設置NFCAdapter
public void onPause() {super.onPause();mAdapter.disableForegroundDispatch(this); }public void onResume() {super.onResume();mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray); }當然之前要定義NfcAdapter
?
nfcAdapter = NfcAdapter.getDefaultAdapter(this); import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.os.Bundle; import android.os.Parcelable; import android.provider.Settings; import android.util.Log; import android.widget.Toast;import com.hwuao.ocis.R; import com.hwuao.ocis.components.easyAndroid.BaseActivity; import com.hwuao.ocis.components.easyAndroid.ContentView; import com.hwuao.ocis.components.easyAndroid.tool.AppLogger; import com.hwuao.ocis.event.GetNFCCardEvent; import com.hwuao.ocis.util.StringUtils; import com.hwuao.ocis.util.ToastUtil;/*** NFC響應頁面** @author Jinyang*/ @SuppressLint("NewApi") @ContentView(R.layout.activity_nfc) public class NFCActivity extends BaseActivity {public static final String TAG = "NFCActivity";private NfcAdapter nfcAdapter;private String mClientCardNfcId;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initNfcParse();resolveIntent(getIntent());}private void initNfcParse() {nfcAdapter = NfcAdapter.getDefaultAdapter(this);if (nfcAdapter == null) {AppLogger.e("---->Nfc error !!!");Toast.makeText(getApplicationContext(), "不支持NFC功能!", Toast.LENGTH_SHORT).show();} else if (!nfcAdapter.isEnabled()) {AppLogger.e("---->Nfc close !!!");Toast.makeText(getApplicationContext(), "請打開NFC功能!", Toast.LENGTH_SHORT).show();}}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);resolveIntent(intent);}@Overridepublic void onResume() {super.onResume();AppLogger.e("---->onDestroy");if (nfcAdapter != null) {if (!nfcAdapter.isEnabled()) {showWirelessSettingsDialog();}}}private void showWirelessSettingsDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("不支持此卡");builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialogInterface, int i) {Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);startActivity(intent);}});builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialogInterface, int i) {finish();}});builder.create().show();return;}private void resolveIntent(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 tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);String nfcId = dumpTagData(tag);if (!nfcId.isEmpty()) {mClientCardNfcId = nfcId;Log.i(TAG, "卡的內容" + mClientCardNfcId);eventBus.post(new GetNFCCardEvent(mClientCardNfcId));finish();} else {ToastUtil.showToastLong(getApplicationContext(), "識別失敗!請重新刷卡!");}}}private String dumpTagData(Parcelable p) {Tag tag = (Tag) p;byte[] id = tag.getId();return StringUtils.hexToHexString(id);}}
這個問題的解決體會到一件事,這個問題在國內的博客論壇都沒有搜索到,在StackOverFlow上有一篇
http://stackoverflow.com/questions/16542147/how-to-read-nfc-tags-to-prevent-system-dialogs-nfc-starter-list-and-tags-being-d
里面的鏈接指向的是Android develop。。。NFC foreground dispatching.?
所有的基礎教程都可以在developer中找到,以后再也不瞎折騰了,有問題找google
源碼請見
https://github.com/xujinyang/NFCALL
總結
以上是生活随笔為你收集整理的当前应用拦截NFC响应,不弹出选择框教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新款奔驰S400L改装原厂360全景影像
- 下一篇: 黑科技揭秘:面对海量的文本翻译任务,阿里