app封装一个简单nfc工具类操作介绍

NFC(Near Field Communication)是一种无线通信技术,它允许设备之间近距离交换数据。在移动设备领域,NFC被广泛应用于支付、身份验证、门禁控制等场景。本文将介绍如何封装一个简单的NFC工具类,用于实现NFC功能的快速开发。

首先,我们需要了解NFC的工作原理。NFC使用近场感应器(Near Field Induction 安卓封装打包工具在哪Sensor)来收发射频信号,它支持三种工作模式:卡模拟器(Card Emulation)、点对点(Peer-to-Peer)和读写器(Reader/Writer)。在Android平台上,我们一般使用读写器模式进行NFC通信。

接下来,让我们来创建一个NFC工具类。

“`java

public class NfcUtils {

private NfcAdapter mAdapter;

private PendingIntent mPendingIntent;

private IntentFilter[] mIntentFilters;

private String[][] mTechLists;

private Context mContext;

public NfcUtils(Context context) {

mContext = context;

mAdapter = NfcAdapter.getDefaultAdapter(mContext);

mPendingIntent = PendingIntent.getActivity(mContext, 0,

new Intent(mContext, mContext.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

mIntentFilters = new IntentFilter[]{ndef};

mTechLists = new String[][]{

new String[]{Ndef.class.getName()},

new String[]{NdefFormatable.class.getName()}

};

}

public void enableForegroundDispatch(Activity activity) {

mAdapter.enableForegroundDispatch(activity, mPendingIntent, mIntentFilters, mTechLists);

}

public void disableForegroundDispatch(Activity activity) {

mAdapter.disableForegroundDispatch(activity);

}

public void processIntent(Intent intent) {

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

if (rawMsgs != null) {

NdefMessage[] msgs = new NdefMessage[rawMsgs.length];

for (int i = 0; i

msgs[i] = (NdefMessage) rawMsgs[i];

}

// 处理NDEF消息

handleNdefMessages(msgs);

}

}

}

private void handleNdefMessages(NdefMessage[] msgs) {

for (NdefMessage msg : msgs) {

NdefRecord[] records = msg.getRecords();

for (NdefRecord record : records) {

// 处理NDEF记录

handleNdefRecord(record);

}

}

}

private void handleNdefRecord(NdefRecord record) {

byte[] payload = record.getPayload();

String textEncoding = ((payload[0] & 0x80) == 0) ? “UTF-8” : “UTF-16”;

int langCodeLen = payload[0] & 0x3F;

try {

String text = new String(payload, langCodeLen + 1, payload.length – langCodeLen – 1, textEncoding);

// 处理NDEF记录内容

handleText(text);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

private void handleText(String text) {

// 处理文本内容

Log.i(“NfcUtils”, “Text: ” + text);

}

}

“`

上述NfcUtils类封装了NFC相关的操作方法,包括启用前台调度(enableForegroundDispatch)、禁用前台调度(disableForegroundDispatch)、处理NFC意图(processIntent)等。

在使用NfcUtils时,需要在Activity的生命周期方法中进行调用。

“`java

public class MainActivity extends AppCompatActivity {

private NfcUtils mNfcUtils;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mNfcUtils = new NfcUtils(this);

// 其他初始化操作…

handleNfcIntent(getIntent());

}

@Override

protected void onResume() {

super.onResume();

mNfcUtils.enableForegroundDispatch(this);

}

@Override

protected void onPause() {

super.onPause();

mNfcUtils.disableForegroundDispatch(this);

}

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

handleNfcIntent(intent);

}

private void handleNfcIntent(Intent intent) {

mNfcUtils.processIntent(intent);

}

}

“`

在MainActivity中,我们需要初始化NfcUtils,并网址一键打包app封装网站工具在合适的生命周期方法中启用前台调度。同时,我们还需要重写onNewIntent方法,以处理新的NFC意图。

以上就是一个简单的NFC工具类的封装过程。通过该工具类,我们可以方便地开发NFC相关的功能,并且可以根据实际需求进行扩展。

请注意,由于NFC功能的特殊性,使用NFC时还需要相关权限配置和设备的支持,请参考Android官方文档进行配置和测试。

小米开发平台应用删除前台服务通知导致的闪退问题说明

小米开发平台应用删除前台服务通知导致的闪退问题说明

  • 近期发现较多应用出现闪退现象,具体trace如下:
AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: Process: com.xxx.app, PID: 25339
AndroidRuntime: java.lang.SecurityException: Not allowed to delete channel XXX with a foreground service
AndroidRuntime:         at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
AndroidRuntime:         at android.os.Parcel.createException(Parcel.java:2360)
AndroidRuntime:         at android.os.Parcel.readException(Parcel.java:2343)
AndroidRuntime:         at android.os.Parcel.readException(Parcel.java:2285)
AndroidRuntime:         at android.app.INotificationManager$Stub$Proxy.deleteNotificationChannel(INotificationManager.java:4040)
AndroidRuntime:         at android.app.NotificationManager.deleteNotificationChannel(NotificationManager.java:909)
AndroidRuntime:         at androidx.core.app.NotificationManagerCompat.deleteNotificationChannel(SourceFile:2)
AndroidRuntime:         at com.gyf.cactus.ext.c$a.run(SourceFile:1)
AndroidRuntime:         at android.os.Handler.handleCallback(Handler.java:938)
AndroidRuntime:         at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime:         at android.os.Looper.loop(Looper.java:236)
AndroidRuntime:         at android.app.ActivityThread.main(ActivityThread.java:8142)
AndroidRuntime:         at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime:         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
AndroidRuntime:         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
AndroidRuntime: Caused by: android.os.RemoteException: Remote stack trace:
AndroidRuntime:         at com.android.server.notification.NotificationManagerService$10.enforceDeletingChannelHasNoFgService(NotificationManagerService.java:3427)
AndroidRuntime:         at com.android.server.notification.NotificationManagerService$10.deleteNotificationChannel(NotificationManagerService.java:3440)
AndroidRuntime:         at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:1737)
AndroidRuntime:         at android.os.Binder.execTransactInternal(Binder.java:1160)
AndroidRuntime:         at android.os.Binder.execTransact(Binder.java:1129)
  • 原因是受Google的一笔安全校验Patch影响:https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java;l=3413
private void enforceDeletingChannelHasNoFgService(String pkg, int userId,
                String channelId) {
            if (mAmi.hasForegroundServiceNotification(pkg, userId, channelId)) {
                Slog.w(TAG, "Package u" + userId + "/" + pkg
                        + " may not delete notification channel '"
                        + channelId + "' with fg service");
                throw new SecurityException("Not allowed to delete channel " + channelId
                        + " with a foreground service");
            }
        }

也就是Google限制了存在前台服务时,不允许应用删除与前台服务绑定的channel。

以上,请开发者检查处理,感谢支持!