xxxxxxxxxx
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
public class YourActivity extends Activity {
private static final int NFC_ENABLE_REQUEST_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Replace 'activity_main' with your layout file
// Check if NFC is available on the device
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// NFC is not available on this device
// Handle the case where NFC is not supported
// For example, you can show an error message or disable NFC-related functionality
return;
}
// Check if NFC is enabled
if (!nfcAdapter.isEnabled()) {
// NFC is not enabled, request the user to enable it
Intent enableNfcIntent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
startActivityForResult(enableNfcIntent, NFC_ENABLE_REQUEST_CODE);
} else {
// NFC is enabled, you can proceed with your NFC-related functionality
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NFC_ENABLE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// The user has enabled NFC
// You can now proceed with your NFC-related functionality
} else {
// The user did not enable NFC
// Handle the case where NFC is not enabled
// For example, you can show an error message or disable NFC-related functionality
}
}
}
}