Method NdefRecord.createTextRecord(“en” , “string”) not working below API level 21

前端 未结 2 1261
小蘑菇
小蘑菇 2020-12-04 02:59

This code works fine when I use it on a device with Android Lollipop (5.x) or Marshmallow (6.0):

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NdefMessage          


        
2条回答
  •  借酒劲吻你
    2020-12-04 03:27

    Yes, createTextRecord is introduced in API 21, so you can't call it in the previous versions. https://developer.android.com/reference/android/nfc/NdefRecord.html

    Check if your API level is 21 before to call createTextRecord.

    public NdfeMessage create(String content){
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            NdefRecord record = NdefRecord.createTextRecord("en", content);
            NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
            return msg;
        } else{
            return null;
        }
    }
    

提交回复
热议问题