【问题标题】:NFC enableForegroundDispatch to handle Beam within activityNFC enableForegroundDispatch 以处理活动中的 Beam
【发布时间】:2014-07-11 03:13:14
【问题描述】:

我正在开发的应用程序应该接收一个光束,然后从 onResume 调用 processIntent(intent) 函数。我遇到的问题是,当它接收到光束时,它会打开一个全新的应用程序实例,而不是仅仅停留在活动中(我已经调用了 enableForegroundDispatch)。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null){
        Toast.makeText(this, "No NFC on this device", Toast.LENGTH_LONG).show();
    }
    // Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    // Declare intent filters to handle the intents that the developer wants to intercept.
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/com.sample.mime/string");
    } catch (MalformedMimeTypeException e) {
        Log.wtf("mimeexception", e);
        e.printStackTrace();
    }
    intentFiltersArray = new IntentFilter[] {ndef};

... ...
}

public void onResume() {
  super.onResume();
  // Enable the foreground dispatch when the Activity regains focus.
  NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);

  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
  }
}

public void onPause() {
  super.onPause();
  // Disable the foreground dispatch when the Activity loses focus.
  NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

void processIntent(Intent intent){
    // do stuff
}

这是清单中的活动

<activity
        android:name="com.example.app.opensthisonebutshouldnt"
        android:label="@string/app_name"  >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name="com.example.app.shouldopenthisone"
        android:label="@string/title_activity_create_invoice"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />\
            <category android:name="android.intent.category.DEFAULT" />\
            <data android:mimeType="application/com.example.nfctest/invoice" />
        </intent-filter>
</activity>

感谢您的意见

【问题讨论】:

  • 最好覆盖 onNewIntent() 如果您的活动在那种情况下打开,它将被调用..!

标签: android nfc intentfilter ndef android-beam


【解决方案1】:
  1. application/com.sample.mime/string(用于ndef.addDataType(...);)看起来不是有效的 MIME 类型。你确定你是 Beaming 这种类型的吗?即使您这样做,我也不确定 Android 是否能够处理它。与您接近的有效 MIME 类型是 application/com.sample.mime(没有 /string 部分)。这同样适用于application/com.example.nfctest/invoice/invoice 部分。

  2. 如果您的前台调度意图过滤器与 Beamed NDEF 消息的第一条记录不匹配,您的应用可能会根据清单中的意图过滤器接收 NFC 事件。请注意,NDEF_DISCOVERED 意图过滤器仅匹配 NDEF 消息的第一条记录

  3. 如果 Beamed 消息包含 AAR(Android 应用程序记录),并且您的 NFC 相关 Intent 过滤器均不匹配 Beamed NDEF 消息的第一条记录,则您在将启动具有用于动作 MAIN 和类别 LAUNCHER 的意图过滤器的清单。 这似乎发生在你的情况。所以仔细检查你的前台调度实际上与传入的 NDEF 消息匹配。否则,您的 Activity 可能会根据您的应用清单中的定义重新启动。

  4. onResume() 中检查getIntent() 只会在您的活动恢复时间>。但是,来自您注册的前台调度的事件将到达您的活动的onNewIntent() 方法。因此,您需要重写该方法并在那里处理 NFC 事件:

    @Override
    public void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            processIntent(intent);
        }
    }
    

【讨论】:

  • 就是这样,MIME 类型错误,因此没有被正确捕获。
猜你喜欢
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多