Android: Intent Filter for NDEF_DISCOVERED在nfc窗口打开应用,而不是运行应用



我实现了这个例子:https://developer.android.com/guide/topics/connectivity/nfc/nfc#p2p

package com.example.android.beam;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.Charset;

public class Beam extends Activity implements CreateNdefMessageCallback {
    NfcAdapter nfcAdapter;
    TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView) findViewById(R.id.textView);
        // Check for available NFC Adapter
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (nfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        // Register callback
        nfcAdapter.setNdefPushMessageCallback(this, this);
    }
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("Beam me up, Android!nn" +
                "Beam Time: " + System.currentTimeMillis());
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMime(
                        "application/vnd.com.example.android.beam", text.getBytes())
         /**
          * The Android Application Record (AAR) is commented out. When a device
          * receives a push with an AAR in it, the application specified in the AAR
          * is guaranteed to run. The AAR overrides the tag dispatch system.
          * You can add it back in to guarantee that this
          * activity starts when receiving a beamed message. For now, this code
          * uses the tag dispatch system.
          */
          //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });
        return msg;
    }
    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }
    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }
    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        textView = (TextView) findViewById(R.id.textView);
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        textView.setText(new String(msg.getRecords()[0].getPayload()));
    }
}
并在Manifest 中添加了这个intent-filter
<intent-filter>
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <data android:mimeType="text/plain"/>
</intent-filter>

当我现在光束在两个设备之一,意图执行代码在processIntent(),但在一个程序称为NFC,而不是在我运行的应用程序。所以我怎么能得到意图在运行的应用程序中执行,而不是打开NFC应用程序?

您正在创建的消息之间有不匹配的mime类型,该消息具有application/vnd.com.example.android.beam的mime类型和text/plain清单中的mime类型,您已要求系统启动您的应用程序,如果它看到。

请注意,如果你匹配mime类型,这段代码很可能取决于你的应用程序的启动模式,开始你接收应用程序,但应用程序不读取NFC消息。

这是因为onNewIntent只在应用程序重新启动时被调用,而你的应用程序没有被重新启动。

正常做法是清单过滤器用Intent启动你的应用程序,你需要在应用程序启动时处理这个(通常在onCreate中)。
onNewIntent仅在您的应用程序已经运行并且使用enableForegroundDispatch让系统发送NFC Intent s到您正在运行的应用程序而不是尝试启动您的应用程序的新实例时使用。

onCreate的末尾加上

processIntent(getIntent());

相关内容

最新更新