我刚刚完成了基于NFC的应用程序。在这里,我只是扫描NFC标签并获取NFC的序列号。
但是当NFC标签靠近设备时,它会显示所有可以扫描NFC标签的应用程序列表,我可以通过“始终”或“默认”来设置,但我希望以编程方式进行此操作。
我正在寻找这个,因为它似乎在一些看似不起作用的设备中出错。
设备中的错误:当NFC标签靠近设备时,我有两个设备甚至没有在对话框中显示“始终”或“默认为操作”。
见截图:
<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>
NFCActivity.java
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);
}
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
@Override
protected void onPause() {
super.onPause();
mNFCAdapter.disableForegroundDispatch(this);
}
@Override
public void onResume() {
super.onResume();
try {
mNFCAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
null);
Tag mTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte byteId[] = mTag.getId();
int size = byteId.length;
// Convert the byte array to integer
String str = "";
if (size > 0) {
for (int i = 0; i < size; i++) {
byte myByte = byteId[i];
int myInt = (int) (myByte & 0xFF);
str += myInt;
}
}
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(
"yyyyMMddhhmmss");
SharedPreferences mSharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
boolean mRegistrationStatus = mSharedPreferences.getBoolean(
"registration_status", false);
boolean mExpiredStatus = mSharedPreferences.getBoolean(
"expire_status", true);
Editor mEditor = mSharedPreferences.edit();
if (mRegistrationStatus && !mExpiredStatus) {
// Tag here started to scan the the NFC tags
mEditor.putString("message", "Tag Read in");
mEditor.commit();
if (Util.isDeviceConnectedWithInternet(this)) {
// Upload the NFC details.
// Start the service and send the NFC Tag Info in the
// intent.
Intent serviceIntent = new Intent(this,
UploadNFCTagInfoService.class);
serviceIntent.putExtra("mNFCSerialNumber", str);
serviceIntent.putExtra("mNFCScannedTimeStamp",
mSimpleDateFormat.format(new Date()));
startService(serviceIntent);
} else {
// Device is not connected with the Internet.
// Store the NFC Tag details in the SQLite database.
// When device connect with Internet later then
// these records will be uploaded on the database server.
Toast.makeText(this, getString(R.string.network_message), Toast.LENGTH_LONG).show();
mEditor.putString("message", "Tag has been saved in the application database to upload it later if your device is activated");
mEditor.commit();
NFCIItem mNFCItem = new NFCIItem();
mNFCItem.setSerialNumber(str);
mNFCItem.setScanTimeStamp(mSimpleDateFormat
.format(new Date()));
// Insert the record in the database.
DatabaseManager.getInstance().addNFCTag(mNFCItem);
}
} else if(!mRegistrationStatus) {
mEditor.putString("message", "Device is not activated to scan NFC Tags");
mEditor.commit();
}
finish();
} catch (Exception e) {
finish();
}
}
}
答案 0 :(得分:1)
我唯一知道如何解决此问题的方法是在您的代码上添加额外的AAR record(特殊NDEF记录)。随后,只有你的应用程序正在启动,没有人再问了。
答案 1 :(得分:0)
你做不到。如果应用程序已打开,您可以使用前台调度使您的应用程序成为首选应用程序(您似乎已经在使用它)。您可以做的最好是调整NFC技术过滤器以尽可能匹配标签(技术+ NDEF等)。对于未显示“始终”选项的设备,如果在对话框中双击应用程序会发生什么?