How to enable NFC setting

流过昼夜 提交于 2021-01-27 04:15:34

问题


I am able to read and wirte in my NFC demo app but I need to check if the NFC option in setting menu is enabled or not . If its not enable I want to direct the user to the setting menu(I know how to do this) similar to NFC TagWriter by NXP. enter image description here

In my application I am using the following SDK version

<uses-sdk android:minSdkVersion="7" />
<uses-sdk android:maxSdkVersion="16"/>

I am unable to check if the setting is enabled or not.


回答1:


TNR gets it right, however also note that from Android version 16, there is a more specific settings action for NFC:

protected void startNfcSettingsActivity() {
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));
        } else {
            startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
        }
    }



回答2:


Use the below code to get the NFCAdapter.

NfcAdapter nfcAdpt = NfcAdapter.getDefaultAdapter(this);
if(nfcAdpt!=null)
{
if(nfcAdpt.isEnabled())
{
//Nfc settings are enabled
}
else
{
//Nfc Settings are not enabled
}
}

If you want to navigate user to Settings of NFC then use below Code

Intent setnfc = new Intent(Settings.ACTION_WIRELESS_SETTINGS);                             
startActivity(setnfc);

There is no NFC for API version 7. So change you manifest file as below

<uses-sdk android:minSdkVersion="10" />
<uses-sdk android:maxSdkVersion="16"/>



回答3:


    if (Build.VERSION.SDK_INT >= 10) {
        i = new Intent("android.settings.NFC_SETTINGS");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(i);
    } else {
        i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(i);
    }


来源:https://stackoverflow.com/questions/14957691/how-to-enable-nfc-setting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!