NFC BroadcastReceiver的问题问题、NFC、BroadcastReceiver

2023-09-05 10:23:40 作者:千年轮回べ只守这份情

我想我的应用程序,听取仅NFC标签时被激活。为此,我想注册一个NFC监听如下,没有任何成功。

I want my app to listen to nfc tags only when is activated. For this I tried to register an nfc listener as following, without any success.

IntentFilter filter = new IntentFilter("android.nfc.action.TECH_DISCOVERED"); 
registerReceiver(nfcTagListener, filter); 

BroadcastReceiver nfcTagListener = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 

            if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
                Log.d("nfc", "" + tag.getId());     
            }
        }
    };

我试图也宣布继apidemos的意图在我的表现和完美的作品,它会启动我的活动,并得到了NFC标签的ID。但是,这不是我想要的,我要检测的标签ID,只有当我的活动中。我想这可能与包含在API演示以下行。但我不知道该怎么做编程

I tried as well to declare the intent in my manifest following the apidemos and works perfectly, it launches my activity and gets the nfc tag id. But this is not what I want, I want to detect the tag id only when I am inside that activity. I am thinking it might be related to the following line included in the api demos. But I dont know how to do that programatically

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

任何提示?

Any hint?

谢谢!

推荐答案

尝试使用前景调度系统。

Try to use Foreground Dispatch System.

要启用它,在活动的onCreate方法,你应该prepare一些东西:

To enable it, on the activity's onCreate method, you should prepare some stuffs:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

在这之后,创建IntentFilters(在我的例子,所有的动作都是用意向过滤器处理):

after that, create the IntentFilters (in my example, all actions are handled using Intent Filters):

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }

    IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    try {
        tech.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }

    IntentFilter[] intentFiltersArray = new IntentFilter[] { tag, ndef, tech };

在这之后,你需要一个String数组包含支持的技术:

after that, you'll need a String array to contain supported technologies:

    String[][] techList = new String[][] { new String[] { NfcA.class.getName(),
            NfcB.class.getName(), NfcF.class.getName(),
            NfcV.class.getName(), IsoDep.class.getName(),
            MifareClassic.class.getName(),
            MifareUltralight.class.getName(), Ndef.class.getName() } };

在onResume方法,你应该使前景调度方式:

in the onResume method, you should enable the foreground dispatch method:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);

和禁用的onPause:

and disable in onPause:

@Override
protected void onPause() {
    super.onPause();
    nfcAdapter.disableForegroundDispatch(this);
}

通过这种方式,你已经成功地初始化所需要的机制。为了处理收到的意图,你应该覆盖onNewIntent(意向意图)方法。

By this way, you have successfully initialized the needed mechanism. To handle a received Intent you should override the onNewIntent(Intent intent) method.

@Override
public void onNewIntent(Intent intent) {
    String action = intent.getAction();

    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
        // reag TagTechnology object...
    } else if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        // read NDEF message...
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

    }
}

请注意:如果你想只前景调度处理的意图,不要让你的manifest文件意图调度系统,只要给予正确的权限,以您的应用程序有

Note: if you want to handle the intents by only foreground dispatching, do not enable Intent Dispatch System in your manifest file, just give the right permissions to your application there.

我希望帮助。