android - 尝试检测 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 的帮助将不胜感激!
解决方案
推荐阅读
- python - 如何编写要求输入 10 个数字的代码
- python - 如何保存 scikit-learn k-means 聚类模型?
- scala - 将 fs2 流输出拆分为两个文件
- c# - 自定义控件内容被使用了两次?
- r - 如何按非字母数字值拆分字符串
- regex - 在 NGINX 位置使用通配符/正则表达式和变量
- python - 使用 OpenCV 与 Matlab 评估校准结果
- angular - 验证电子邮件更改未反映在 Firestore 中的同一会话上
- javascript - 如何在 Electron.js 主窗口页面顶部呈现内容,同时避免 webview 或 browserview
- selenium - 如何使用 selenium webdriver 获取 Id 令牌持有者