下面列出了怎么用android.nfc.cardemulation.CardEmulation的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityLog = (TextView) findViewById(R.id.activity_log);
CardEmulation cardEmulationManager = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
ComponentName paymentServiceComponent =
new ComponentName(getApplicationContext(), PaymentService.class.getCanonicalName());
if (!cardEmulationManager.isDefaultServiceForCategory(paymentServiceComponent, CardEmulation.CATEGORY_PAYMENT)) {
Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
intent.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
intent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, paymentServiceComponent);
startActivityForResult(intent, 0);
log(TAG, "onCreate: Requested Android to make SwipeYours the default payment app");
} else {
log(TAG, "onCreate: SwipeYours is the default NFC payment app");
}
}
@Override
protected void onResume() {
super.onResume();
LogUtil.d(TAG, "\"" + TAG + "\": Activity resume");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mCardEmulation != null && mCardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT)) {
mCardEmulation.setPreferredService(this, new ComponentName(this, PaymentHostApduService.class));
}
}
}
@Override
protected void onPause() {
super.onPause();
LogUtil.d(TAG, "\"" + TAG + "\": Activity pause");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mCardEmulation != null && mCardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT)) {
mCardEmulation.unsetPreferredService(this);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
this.context = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
ButterKnife.bind(this);
cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(getApplicationContext()));
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setAsPreferredHceService() {
boolean allowsForeground = cardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT);
if (allowsForeground) {
ComponentName hceComponentName = new ComponentName(context, HandstandApduService.class);
cardEmulation.setPreferredService(PayActivity.this, hceComponentName);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void unsetAsPreferredHceService() {
boolean allowsForeground = cardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT);
if (allowsForeground) {
ComponentName hceComponentName = new ComponentName(context, HandstandApduService.class);
cardEmulation.unsetPreferredService(PayActivity.this);
}
}
public static void ensureSetAsDefaultPaymentApp(Activity context) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
CardEmulation cardEmulation = CardEmulation.getInstance(nfcAdapter);
ComponentName componentName = new ComponentName(context, HandstandApduService.class);
boolean isDefault = cardEmulation.isDefaultServiceForCategory(componentName, CardEmulation.CATEGORY_PAYMENT);
if (!isDefault) {
Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
intent.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
intent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, componentName);
context.startActivityForResult(intent, REQUEST_CODE_DEFAULT_PAYMENT_APP);
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "HCE service created");
CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), AID);
if(!defaultService) {
throw new IllegalArgumentException("Expected default service for AID " + AID);
}
Log.d(TAG, "HCE service AID is " + AID);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
setContentView(R.layout.activity_main);
CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), EchoHostApduService.AID);
if (!defaultService) {
Log.w(TAG, "Expected default service for AID " + EchoHostApduService.AID);
}
Log.d(TAG, "Service AID is " + EchoHostApduService.AID);
enableBroadcast();
showHelpfulDialog();
Intent intent = getIntent();
hostCardEmulationBroadcastReceiver.onReceive(this, getIntent());
}