Java 点击另一个支持NFC的设备后无法访问onNewIntent。(android)
主要问题是,当我用其他支持NFC的手机(NFC已打开)轻触手机时,无法转到onNewIntent()。 除主要意图外,在任何情况下,我都无法到达onNewIntent。我试过NDEF、TECH和TAG三种过滤器Java 点击另一个支持NFC的设备后无法访问onNewIntent。(android),java,android,android-intent,nfc,onnewintent,Java,Android,Android Intent,Nfc,Onnewintent,主要问题是,当我用其他支持NFC的手机(NFC已打开)轻触手机时,无法转到onNewIntent()。 除主要意图外,在任何情况下,我都无法到达onNewIntent。我试过NDEF、TECH和TAG三种过滤器 package com.example.nfctry; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.os.Bundle; import android.app.Activity; impor
package com.example.nfctry;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
adapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] {tagDetected};
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent)
{
Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
// getIntent() should always return the most recent
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this,"DETECTED muahhhhh" + myTag.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在android mainfest中,我还添加了IntentFilter
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfctry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nfctry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
它独立于您使用的标签过滤器。您的
onNewIntent
仅在使用launchMode
:singleTop
或singleTask
调用intent
时调用get,当然,当您自己调用它时。当您的应用程序位于前端时,它不会捕获NDEF/TECH/TAG发现。您需要使用ForegroundDispatching
捕获当前应用中的标签发现事件
当您的ForegroundDispatch
捕获事件并使用pendingent.getActivity
和flag\u ACTIVITY\u SINGLE\u TOP
标记在您的pendingent
中时,它将调用onNewIntent
您应该在onResume
中启用ForegroundDispatch
:
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
并在暂停时禁用它:
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
要使用ForegroundDispatch
捕获当前代码中的标记事件,如下所示:
public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
adapter = NfcAdapter.getDefaultAdapter(this);
onNewIntent(this.getIntent());
}
@Override
public void onNewIntent(Intent data)
{
Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
// getIntent() should always return the most recent
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this,"DETECTED muahhhhh" + myTag.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
还请注意,您应该在清单中添加NFC用户权限。它与您使用的标记过滤器无关。您的
onNewIntent
仅在使用launchMode
:singleTop
或singleTask
调用intent
时调用get,当然,当您自己调用它时。当您的应用程序位于前端时,它不会捕获NDEF/TECH/TAG发现。您需要使用ForegroundDispatching
捕获当前应用中的标签发现事件
当您的ForegroundDispatch
捕获事件并使用pendingent.getActivity
和flag\u ACTIVITY\u SINGLE\u TOP
标记在您的pendingent
中时,它将调用onNewIntent
您应该在onResume
中启用ForegroundDispatch
:
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
并在暂停时禁用它:
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
要使用ForegroundDispatch
捕获当前代码中的标记事件,如下所示:
public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
adapter = NfcAdapter.getDefaultAdapter(this);
onNewIntent(this.getIntent());
}
@Override
public void onNewIntent(Intent data)
{
Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
// getIntent() should always return the most recent
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this,"DETECTED muahhhhh" + myTag.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
还请注意,您应该在清单中添加NFC用户权限。它与您使用的标记过滤器无关。您的
onNewIntent
仅在使用launchMode
:singleTop
或singleTask
调用intent
时调用get,当然,当您自己调用它时。当您的应用程序位于前端时,它不会捕获NDEF/TECH/TAG发现。您需要使用ForegroundDispatching
捕获当前应用中的标签发现事件
当您的ForegroundDispatch
捕获事件并使用pendingent.getActivity
和flag\u ACTIVITY\u SINGLE\u TOP
标记在您的pendingent
中时,它将调用onNewIntent
您应该在onResume
中启用ForegroundDispatch
:
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
并在暂停时禁用它:
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
要使用ForegroundDispatch
捕获当前代码中的标记事件,如下所示:
public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
adapter = NfcAdapter.getDefaultAdapter(this);
onNewIntent(this.getIntent());
}
@Override
public void onNewIntent(Intent data)
{
Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
// getIntent() should always return the most recent
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this,"DETECTED muahhhhh" + myTag.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
还请注意,您应该在清单中添加NFC用户权限。它与您使用的标记过滤器无关。您的
onNewIntent
仅在使用launchMode
:singleTop
或singleTask
调用intent
时调用get,当然,当您自己调用它时。当您的应用程序位于前端时,它不会捕获NDEF/TECH/TAG发现。您需要使用ForegroundDispatching
捕获当前应用中的标签发现事件
当您的ForegroundDispatch
捕获事件并使用pendingent.getActivity
和flag\u ACTIVITY\u SINGLE\u TOP
标记在您的pendingent
中时,它将调用onNewIntent
您应该在onResume
中启用ForegroundDispatch
:
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
并在暂停时禁用它:
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
要使用ForegroundDispatch
捕获当前代码中的标记事件,如下所示:
public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
adapter = NfcAdapter.getDefaultAdapter(this);
onNewIntent(this.getIntent());
}
@Override
public void onNewIntent(Intent data)
{
Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
// getIntent() should always return the most recent
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this,"DETECTED muahhhhh" + myTag.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume()
{
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
IntentFilter[] intentFilters = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
adapter.enableForegroundDispatch( this,
pendingIntent,
intentFilters,
new String[][]{
new String[]{"android.nfc.tech.NfcA"}
});
}
@Override
public void onPause() {
super.onPause();
if (adapter != null)
{
try {
adapter.disableForegroundDispatch(this);
}
catch (NullPointerException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
还请注意,您应该在清单中添加NFC用户权限。请检查此处嗨,Michael,您的代码正常工作,但我的NFC卡使用NFCA技术,因此toast显示“卡类型不受支持”。要添加此类型,我可以做些什么?检查这里嗨,Michael,您的代码正在工作,但我的NFC卡使用NFCA技术,因此toast显示“卡类型不受支持”。要添加此类型,我可以做些什么?检查这里嗨,Michael,您的代码正在工作,但我的NFC卡使用NFCA技术,因此toast显示“卡类型不受支持”。要添加此类型,我可以做些什么?检查这里嗨,Michael,您的代码正在工作,但我的NFC卡使用NFCA技术,因此toast显示“卡类型不受支持”。如何添加此类型?谢谢,我得到了错误。但是现在每当我试图获取ID时,每次我都会获得不同的ID。NdefMessage[]msgs=getTechMessages(getIntent());NdefRecord record=msgs[0].getRecords()[0];字节[]有效负载=record.getId();有效负载给出“null”,而getID()给出一些排序的随机值[B@426fd2a0“。我们的nfc标签中没有数据,主要目的只是获取一个唯一的ID。
[B@426fd2a0
不是真正的ASCII输出。您应该使用新字符串(有效负载)打印它
。您不应该也获取NDEF记录的ID,因为它不会返回