本文整理汇总了Java中android.nfc.NfcAdapter类的典型用法代码示例。如果您正苦于以下问题:Java NfcAdapter类的具体用法?Java NfcAdapter怎么用?Java NfcAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NfcAdapter类属于android.nfc包,在下文中一共展示了NfcAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readNfcTag
import android.nfc.NfcAdapter; //导入依赖的package包/类
/**
* 读取NFC标签文本数据
*/
private void readNfcTag(Intent intent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msgs[] = null;
int contentSize = 0;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
contentSize += msgs[i].toByteArray().length;
}
}
try {
if (msgs != null) {
NdefRecord record = msgs[0].getRecords()[0];
String textRecord = parseTextRecord(record);
mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
}
} catch (Exception e) {
}
}
}
示例2: onNewIntent
import android.nfc.NfcAdapter; //导入依赖的package包/类
@Override
public void onNewIntent(Intent intent)
{
String action = intent.getAction();
// Log.i("!intent! ", action);
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = tag.getId();
String serialNumber = bytesToHex(id);
WritableMap idData = Arguments.createMap();
idData.putString("id", serialNumber);
sendEvent(this.reactContext, "NFCCardID", idData);
}
示例3: onCreate
import android.nfc.NfcAdapter; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter mifare = new IntentFilter((NfcAdapter.ACTION_TECH_DISCOVERED));
filters = new IntentFilter[] { mifare };
techs = new String[][] { new String[] {NfcA.class.getName() } };
if(adapter==null)
{
adapter = NfcAdapter.getDefaultAdapter(this);
}
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
imageView.startAnimation(rotation);
}
});
}
示例4: onNewIntent
import android.nfc.NfcAdapter; //导入依赖的package包/类
@Override
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.hasExtra(NfcAdapter.EXTRA_TAG ))
{
if(BackgroundTask.isNetworkAvailable(HubMain.this))
{
gifImageView.setVisibility(View.INVISIBLE);
spinner.setVisibility(View.VISIBLE);
}
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(parcelables != null && parcelables.length > 0)
{
readTextfromMessage((NdefMessage)parcelables[0]);
}
}
}
示例5: setupForegroundDispatch
import android.nfc.NfcAdapter; //导入依赖的package包/类
/**
* Setup the recognition of nfc tags when the activity is opened (foreground)
*
* @param activity The corresponding {@link Activity} requesting the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Filter for nfc tag discovery
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
示例6: resolveIntent
import android.nfc.NfcAdapter; //导入依赖的package包/类
private void resolveIntent(Intent data) {
this.setIntent(data);
if ((data.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
return;
}
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
mLastNfcTag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
long now = new Date().getTime();
if (mContinuousSensorReadingFlag) {
startContinuousSensorReadingTimer();
} else if (now - mLastScanTime > 5000) {
DataPlotFragment dataPlotFragment = (DataPlotFragment)
mSectionsPagerAdapter.getRegisteredFragment(R.integer.viewpager_page_show_scan);
if (dataPlotFragment != null) {
dataPlotFragment.clearScanData();
}
new NfcVReaderTask(this).execute(mLastNfcTag);
}
}
}
示例7: tryUpdateIntentFromBeam
import android.nfc.NfcAdapter; //导入依赖的package包/类
/**
* Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
* an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
* activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
* the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
*/
public static void tryUpdateIntentFromBeam(Activity activity) {
Intent originalIntent = activity.getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// Record 0 contains the MIME type, record 1 is the AAR, if present.
// In iosched, AARs are not present.
NdefRecord mimeRecord = msg.getRecords()[0];
if (ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(
new String(mimeRecord.getType()))) {
// Re-set the activity's intent to one that represents session details.
Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(new String(mimeRecord.getPayload())));
activity.setIntent(sessionDetailIntent);
}
}
}
示例8: handleIntent
import android.nfc.NfcAdapter; //导入依赖的package包/类
private void handleIntent(final Intent intent) {
final String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
final String inputType = intent.getType();
final NdefMessage ndefMessage = (NdefMessage) intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);
new BinaryInputParser(inputType, input) {
@Override
protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
cannotClassify(inputType);
}
@Override
protected void error(final int messageResId, final Object... messageArgs) {
dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
}
}.parse();
}
}
示例9: registerTagEvent
import android.nfc.NfcAdapter; //导入依赖的package包/类
@ReactMethod
private void registerTagEvent(String alertMessage, Boolean invalidateAfterFirstRead, Callback callback) {
Log.d(LOG_TAG, "registerTag");
isForegroundEnabled = true;
// capture all mime-based dispatch NDEF
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFilters.add(ndef);
// capture all rest NDEF, such as uri-based
intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
techLists.add(new String[]{Ndef.class.getName()});
// for those without NDEF, get them as tags
intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));
if (isResumed) {
enableDisableForegroundDispatch(true);
}
callback.invoke();
}
示例10: onCreate
import android.nfc.NfcAdapter; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
mAdapter = NfcAdapter.getDefaultAdapter(this);
// Create an NDEF message a URL
mMessage = new NdefMessage(NdefRecord.createUri("http://www.android.com"));
setContentView(R.layout.foreground_dispatch);
mText = (TextView) findViewById(R.id.text);
if (mAdapter != null) {
mAdapter.setNdefPushMessage(mMessage, this);
mText.setText("Tap another Android phone with NFC to push a URL");
} else {
mText.setText("This phone is not NFC enabled.");
}
}
示例11: processIntent
import android.nfc.NfcAdapter; //导入依赖的package包/类
private void processIntent(Intent i) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
Parcelable[] rawMsgs =
i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
String url = new String(msg.getRecords()[0].getPayload());
Utils.debugLog(TAG, "Got this URL: " + url);
Toast.makeText(this, "Got this URL: " + url, Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setClass(this, ManageReposActivity.class);
startActivity(intent);
finish();
}
}
示例12: prepareNfcMenuItems
import android.nfc.NfcAdapter; //导入依赖的package包/类
@TargetApi(16)
private void prepareNfcMenuItems(Menu menu) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
MenuItem menuItem = menu.findItem(R.id.menu_enable_nfc);
if (nfcAdapter == null) {
menuItem.setVisible(false);
return;
}
boolean needsEnableNfcMenuItem;
if (Build.VERSION.SDK_INT < 16) {
needsEnableNfcMenuItem = !nfcAdapter.isEnabled();
} else {
needsEnableNfcMenuItem = !nfcAdapter.isNdefPushEnabled();
}
menuItem.setVisible(needsEnableNfcMenuItem);
}
示例13: setAndroidBeam
import android.nfc.NfcAdapter; //导入依赖的package包/类
@TargetApi(16)
public static void setAndroidBeam(Activity activity, String packageName) {
if (Build.VERSION.SDK_INT < 16) {
return;
}
PackageManager pm = activity.getPackageManager();
NfcAdapter nfcAdapter = getAdapter(activity);
if (nfcAdapter != null) {
ApplicationInfo appInfo;
try {
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
Uri[] uris = {
Uri.parse("file://" + appInfo.publicSourceDir),
};
nfcAdapter.setBeamPushUris(uris, activity);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Could not get application info", e);
}
}
}
示例14: onCreate
import android.nfc.NfcAdapter; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hub_main);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
setTitle("Tap In Hub");
yeeid = (TextView)findViewById(R.id.yeeid);
spinner = (ProgressBar)findViewById(R.id.progressBar);
gifImageView = (GifImageView)findViewById(R.id.gifImageView);
try
{
InputStream inputStream = getAssets().open("nfc.gif");
byte[] bytes = IOUtils.toByteArray(inputStream);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
}
catch (IOException e)
{}
gifImageView.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
}
示例15: onCreate
import android.nfc.NfcAdapter; //导入依赖的package包/类
/**
* This activity usually be loaded from the starting screen of the app.
* This method handles the start-up of the activity, it does not need to call any other methods
* since the activity onNewIntent() calls the intentHandler when a NFC chip is detected.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
documentData = (DocumentData) extras.get(DocumentData.identifier);
thisActivity = this;
setContentView(R.layout.activity_passport_con);
Toolbar appBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(appBar);
Util.setupAppBar(appBar, this);
TextView notice = (TextView) findViewById(R.id.notice);
progressView = (ImageView) findViewById(R.id.progress_view);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
checkNFCStatus();
notice.setText(R.string.nfc_enabled);
}