【发布时间】:2014-02-24 02:14:45
【问题描述】:
我刚刚完成了基于 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();
}
}
}
【问题讨论】: