【发布时间】:2015-12-21 01:58:59
【问题描述】:
这与question 相似,但不完全相同。我不是在问如何在运行时请求权限。
我正在使用 android 中的 nfc,因此我需要类似的权限
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
但是,在三星(SM-N900L)中,我只需要使用来捕获 newIntent
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
resolveIntent(intent);
}
基本上,我的问题是,为什么在三星我不需要意图过滤器,但在其他手机中我需要过滤器?
前台配置
@Override
protected void onResume() {
super.onResume();
if (hasNfcAdapter()) {
if (!mAdapter.isEnabled()) {
Toast.makeText(this, "NFC is disabled.", Toast.LENGTH_LONG).show();
}
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
if (hasNfcAdapter())
mAdapter.disableForegroundDispatch(this);
}
protected void initNfc() {
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter != null) {
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
protected boolean hasNfcAdapter() {
return mAdapter != null;
}
【问题讨论】:
-
这是您所有与 NFC 相关的代码吗?会不会是你注册前台调度系统之类的?
-
@MichaelRoland 很抱歉回复晚了,是的,我注册了前台调度,我认为有必要吗?我刚刚关注了documentation
-
当您希望应用通过 NFC 标签启动时,您只需要 AndroidManifest.xml 中的意图过滤器。如果您想在您的活动在前台处于活动状态时检测事件,那么前台调度就足够了。因此,如果您的应用在某些设备上没有 Intent 过滤器就无法运行,那么您很可能在配置前台调度时出错。
-
@MichaelRoland 好的,但事情是这样的,我在多个设备上尝试了我的应用程序,但除了三星之外它无法正常工作。当我尝试放置意图过滤器时,它起作用了。另外,我从 android 文档中获得了前台配置。
-
嗯,这将是因为所有 其他 OEM,因为三星设备上的行为是预期的行为。