【问题标题】:onNewIntent Method is not called when NFC Tag is scanned扫描 NFC Tag 时不调用 onNewIntent 方法
【发布时间】:2019-01-16 19:07:40
【问题描述】:

当我用手机扫描 NFC 标签时,不会调用 onNewIntent() 方法。它只是打开一个栏,我可以在其中选择应该处理扫描的应用程序,但即使我在那里选择我的应用程序,onNewIntent() 方法也不会执行。

我已经尝试将 NFC 标签处理放在一个名为 performTagOperations() 的额外方法中,

主活动:

public class MainActivity extends AppCompatActivity {
    TextView mtv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mtv1 = findViewById(R.id.tv1);
        mtv1.setText("Hallo");
        performTagOperations(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        Toast.makeText(this,"Intent",Toast.LENGTH_LONG).show();
        mtv1.setText("Intent");
        performTagOperations(intent);
    }

    private void performTagOperations(Intent intent){
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            Log.d("NFC",tag.toString());
            Parcelable[] rawMessages =
                    intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMessages != null) {
                NdefMessage[] messages = new NdefMessage[rawMessages.length];
                for (int i = 0; i < rawMessages.length; i++) {
                    messages[i] = (NdefMessage) rawMessages[i];
                }
                // Process the messages array.
                for (NdefMessage n:
                        messages) {
                    Log.d("NFC", n.toString());
                }
            }
        }
    }
}

AndroidManifest:

<uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>

nfc_tech_filter:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <!-- class name -->
    </tech-list>
</resources>

扫描标签时应该执行onNewIntent()方法,但不执行。

【问题讨论】:

    标签: android android-intent nfc intentfilter ndef


    【解决方案1】:

    您已注册以在清单中接收 NFC 意图 android.nfc.action.<b>TECH_</b>DISCOVERED。但是,在performTagOperations() 中,您希望收到android.nfc.action.<b>NDEF_</b>DISCOVERED(NfcAdapter.ACTION_<b>NDEF_</b>DISCOVERED)。因此,该 IF 语句中的代码将永远不会执行。您必须将其更改为

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
    }
    

    或者更好的是,为适合您的标签的 NDEF 数据类型注册一个意图过滤器。

    还请注意,onNewIntent() 只会在您的活动已经运行时被调用。如果您的活动是由 NFC 意图创建的,您可以在例如onCreate() 代替。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      相关资源
      最近更新 更多