首页 > 解决方案 > 尝试检测 NFC 但未调用 onNewIntent

问题描述

我正在尝试在 Android Studio 上的 RF430FRL152H 中检测我的 NFC。我的目标是,如果检测到 NFC,则从 main 中打开一个新活动。到目前为止,这是我的主要内容:

public class MainActivity extends AppCompatActivity {
    //Initialize the attributes
    NfcAdapter nfcAdapter;
    PendingIntent pendingIntent;
    final static String TAG = "nfc_test";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initialise NfcAdapter
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        //If no NfcAdapter, display that the device has no NFC
        if (nfcAdapter == null){
            Toast.makeText(this,"NO NFC Capabilities",
                    Toast.LENGTH_SHORT).show();
        }
        Log.d("nfc_capable", "Phone can use NFC");
        //Create a PendingIntent object so the Android system can
        //populate it with the details of the tag when it is scanned.
        //PendingIntent.getActivity(Context,requestcode(identifier for
        //                           intent),intent,int)
        pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        resolveIntent(intent);
    }
    public void resolveIntent(Intent newIntent){
        String action = newIntent.getAction();
        Log.d("myTag", action);
        newIntent = new Intent(this, NFC_Info.class);
        if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
            startActivity(newIntent);
        }
    }

我的清单检查 NFC 功能以及意图过滤器。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app_2">

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.App_2">
        <activity android:name=".graph_trial2"></activity>
        <activity
            android:name=".TrialHeader2"
            android:label="@string/title_activity_trial_header2"
            android:theme="@style/Theme.App_2.NoActionBar" />
        <activity android:name=".NFC_Info" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

问题是当我打开应用程序时,当我的手机在 NFC 上嗡嗡作响时,没有调用 onNewIntent,因此什么也没有发生。任何有关检测 NFC 的帮助将不胜感激!

标签: androidandroid-studionfctexas-instruments

解决方案


推荐阅读