简体   繁体   English

Android:NDEF_DISCOVERED 的意图过滤器在 nfc window 中打开应用程序,而不是运行应用程序

[英]Android: Intent Filter for NDEF_DISCOVERED opens app in nfc window instead of running app

I implemented this example: https://developer.android.com/guide/topics/connectivity/nfc/nfc#p2p我实现了这个例子: 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!\n\n" +
                "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()));
    }
}

in my app and added this intent-filter to the Manifest在我的应用程序中并将此意图过滤器添加到清单中

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

When i now beam on one of the two devices, the intent executes the code in processIntent(), but inside of a program called NFC and not in my running app.当我现在在两台设备中的一台上传输时,意图执行 processIntent() 中的代码,但在名为 NFC 的程序内部,而不是在我正在运行的应用程序中。 So how can I get the Intent to execute in the running app and not to open the NFC application?那么如何让 Intent 在正在运行的应用程序中执行而不打开 NFC 应用程序呢?

You have non matching mime Types between the message you are creating which has mime Type of application/vnd.com.example.android.beam and the mime Type in the Manifest of text/plain you have asked the system to start you App if it sees.您正在创建的消息之间存在不匹配的 mime 类型,该消息的 mime 类型为application/vnd.com.example.android.beamtext/plain清单中的 mime 类型,如果它已要求系统启动您的应用程序看到。

Note that if you do match the mime Types, with this code it is likely depending on your App's launch mode to start you receiving App but for the App to not read the NFC message.请注意,如果您确实匹配 mime 类型,则使用此代码可能取决于您的应用程序的启动模式来启动您接收应用程序,但应用程序不读取 NFC 消息。

This is because onNewIntent is only called on Application re-launch and your application is not being re-launched.这是因为onNewIntent仅在应用程序重新启动时调用,并且您的应用程序没有重新启动。

Normal practise is the manifest filter starts you App with an Intent , you need to process this on App start (usually in onCreate ).通常的做法是清单过滤器使用Intent启动您的 App,您需要在 App 启动时处理它(通常在onCreate中)。
onNewIntent is only used when your App is already running and has used enableForegroundDispatch to get the system to send NFC Intent s to your running App instead of trying to start a new Instance of your App. onNewIntent仅在您的应用程序已经运行并且已使用enableForegroundDispatch让系统将 NFC Intent发送到您正在运行的应用程序而不是尝试启动您的应用程序的新实例时使用。

So in your code at the end of onCreate add the line因此,在onCreate末尾的代码中添加该行

processIntent(getIntent());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM