试图检测 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,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 的帮助将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)