简体   繁体   English

方法 onNewIntent(intent) 不在 NFC 中执行

[英]Method onNewIntent(intent) not executing in NFC

I am implementing the NFC in android project.我正在 android 项目中实现 NFC。 I have written the entries which are required in the AndroidManifest.xml and Java code.我已经编写了 AndroidManifest.xml 和 Java 代码中所需的条目。

When any tag come near by device then my app detect the tag then it open the activity but here I am getting NFC Tag Info but onNewIntent is not executing.当设备靠近任何标签时,我的应用程序会检测到该标签,然后它会打开活动,但在这里我正在获取 NFC 标签信息,但onNewIntent未执行。

Please guys share your views.请各位大侠分享一下看法。

public class NFCActivity extends Activity {

    private NfcAdapter mNFCAdapter;
    private PendingIntent mNfcPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);

        // Create the OpenSqliteHelper object. It always best to create only
        // once instance of this OpenSqliteHelper
        DatabaseManager.init(this);

        mNFCAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNFCAdapter == null) {
            // Device does not support NFC
            Toast.makeText(this,
                    getString(R.string.device_does_not_support_nfc),
                    Toast.LENGTH_LONG).show();
        } else {
            if (!mNFCAdapter.isEnabled()) {
                // NFC is disabled
                Toast.makeText(this, getString(R.string.enable_nfc),
                        Toast.LENGTH_LONG).show();
            } else {
                mNfcPendingIntent = PendingIntent.getActivity(NFCActivity.this,
                        0, new Intent(NFCActivity.this, NFCActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            }
        }
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Tag mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mNFCAdapter.disableForegroundDispatch(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mNFCAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
                null);
    }
}

AndroidManifest.xml AndroidManifest.xml

<activity
    android:name="net.livepatrols.thepartnerSA.NFCActivity"
    android:theme="@android:style/Theme.NoDisplay" >
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
    </intent-filter>

    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />

    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Your code works fine when your activity is on foreground, but it won't work when the Activity is started by an Intent with android.nfc.action.XXX_DISCOVERED action.当您的 Activity 在前台时,您的代码可以正常工作,但是当 Activity 由具有android.nfc.action.XXX_DISCOVERED操作的 Intent 启动时,它将无法正常工作。 As it is the first intent, onNewIntent() won't be called, but you can use the intent in your onCreate() method.由于它是第一个意图, onNewIntent()不会调用onNewIntent() ,但您可以在onCreate()方法中使用该意图。 You should call your NFC logic from onCreate() and from onNewIntent() , validating the action before accessing the tag.您应该从onCreate()onNewIntent()调用您的 NFC 逻辑,在访问标签之前验证操作。

public class NFCActivity extends Activity {

    private NfcAdapter mNFCAdapter;
    private PendingIntent mNfcPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        performTagOperations(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        performTagOperations(intent);
    }

    private void performTagOperations(Intent intent){
        String action = intent.getAction();
        if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) ||
        action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) ){
            //PERFORM TAG OPERATIONS
        }
    }
    ...
}

In my case the problem was, that I followed the documentation for nfcAdapter.enableForegroundDispatch(...) blindly and I pasted this code into fragment :就我而言,问题是,我盲目地遵循了nfcAdapter.enableForegroundDispatch(...)的文档, nfcAdapter.enableForegroundDispatch(...)此代码粘贴到片段中

pendingIntent = PendingIntent.getActivity(
            this,
            0,
            new Intent(this, getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

Since it actually expects activity, just change all to this to getContext() and especially getClass() .由于它实际上需要活动,因此只需将所有内容更改为getContext() ,尤其是getClass()

pendingIntent = PendingIntent.getActivity(
            getContext(),
            0,
            new Intent(getContext(), getContext().getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

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

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