gpt4 book ai didi

java - 在我的应用程序中使用 NFC 时如何禁用默认的 Android NFC 应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 07:57:38 25 4
gpt4 key购买 nike

<分区>

在我的应用程序中,我使用 NFC 读取标签。我单击按钮以启用 NFC。打开一个进度对话框以读取 NFC 标签,完成后,NFC 将被禁用。这一切都很好。但是,当应用程序中未启用 NFC 并且我将 NFC 标签放入手机时,默认的 Android 应用程序会读取 NFC 标签并将我的应用程序置于后台。

如何禁用 Android 应用程序?

我启用/禁用 NFC 的代码:

/**
* @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[][]{};

// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException(activity.getString(R.string.exception_wrong_mime_type));
}

adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

/**
* @param activity The corresponding {@link MainActivity} requesting to stop the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com