如何让android应用程序听特定的NFC芯片?

时间:2016-07-19 11:35:54

标签: android broadcastreceiver nfc

我正在创建应用程序,如果点击NFC芯片会做一些动作,我知道以下关于Android的NFC信息如果我错了请纠正我

  1. 您无法在清单文件中向接收者注册NFC的操作,只能注册活动。
  2. 每个NFC芯片都有自己独特的ID。
  3. 我想做的是
    当应用程序处于后台或关闭时,如果我点击带有id(ex 1234)的NFC芯片,我的应用程序会启动并执行某些操作。

    可能吗? 如果是的话,如何实现?

    编辑 这是我的代码,它会在您检查任何NFC芯片时打开并执行此操作android.nfc.action.TECH_DISCOVERED 但当它打开动作时android.intent.action.MAIN MainActivity.java

        public class MainActivity extends AppCompatActivity {
        public static final String ACTION_CHECK_IN= "checked";
        public static final String ACTION_CHECK_OUT= "unchecked";
        private NfcAdapter mNfcAdapter;
        boolean isCheckedIn = false;
    
        private static final String TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
            isCheckedIn = Pref.getBoolean("check", false);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.d(TAG, "onResume() called with: " + "");
            String action = getIntent().getAction();
            if (action != null) {
                Log.d("MainActivity", "onCreate(" + action + ")");
                if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
    
                    if (!isCheckedIn) {
                        isCheckedIn = true;
                        Pref.putBoolean("check", true);
                        Log.d("checking","IN");
                    } else {
                        isCheckedIn = false;
                        Pref.putBoolean("check", false);
                        Log.d("checking","Out");
                    }
    
                }
    
            }
    
            if (!mNfcAdapter.isEnabled()) {
    
                Toast.makeText(MainActivity.this, "Please open it ", Toast.LENGTH_SHORT).show();
            }
    
    
            /**
             * It's important, that the activity is in the foreground.
             * Otherwise an IllegalStateException is thrown.
             */
            setupForegroundDispatch(this, mNfcAdapter);
        }
    
        @Override
        protected void onPause() {
            /**
             * Call this before onPause, otherwise an IllegalArgumentException is thrown.
             */
            stopForegroundDispatch(this, mNfcAdapter);
    
            super.onPause();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    
    
        public static void setupForegroundDispatch(Activity activity, NfcAdapter adapter) {
            final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
            final PendingIntent pendingIntent = PendingIntent.getActivity(
                    activity.getApplicationContext(), 0, intent, 0);
    
            IntentFilter[] filters = new IntentFilter[2];
            String[][] techList = new String[][]{};
    
            // same filter as manifest - action, category
            filters[0] = new IntentFilter();
            filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
            filters[1] = new IntentFilter();
            filters[1].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
    
            adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
        }
    
        public static void stopForegroundDispatch(Activity activity, NfcAdapter adapter) {
            adapter.disableForegroundDispatch(activity);
        }
    }
    

    清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.srjo.pocnfcadapter">
    
        <uses-permission android:name="android.permission.NFC" />
    
        <uses-feature
            android:name="android.hardware.nfc"
            android:required="true" />
    
        <application
            android:name=".MyApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
    
    
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.nfc.action.TAG_DISCOVERED" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.nfc.action.TECH_DISCOVERED" />
                </intent-filter>
    
                <meta-data
                    android:name="android.nfc.action.TECH_DISCOVERED"
                    android:resource="@xml/filter_nfc" />
    
            </activity>
    
        </application>
    
    </manifest>
    

2 个答案:

答案 0 :(得分:0)

根据Android开发者网站的说法,您的应用可以过滤ACTION_NDEF_DISCOVERED等NFC意图。

答案 1 :(得分:0)

首先在清单

中注册您的应用
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.survivingwithandroid.nfc" >

....
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
<manifest>

然后你应该使用前台调度(前台app)。最后,您使用NFCAdapter读取内容(onNewIntent)。 如果你愿意,我写了详细的posts about NFC