我正在開(kāi)發(fā)一個(gè)應(yīng)用程序,并有以下問(wèn)題.
在使用NFC進(jìn)行設(shè)備所有者配置時(shí),我想發(fā)送一個(gè)字符串,該字符串將由新設(shè)備所有者應(yīng)用程序使用.
我知道設(shè)備所有者配置的標(biāo)準(zhǔn)MIME屬性,找到了here
這是一個(gè)片段,可以讓您更好地了解我的問(wèn)題.注意“myCustomValue”屬性.
Properties properties = new Properties();properties.put("myCustomValue", value);properties.put(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, "com.example.some.app");try { properties.store(stream, "NFC Provisioning"); ndefMessage = new NdefMessage(new NdefRecord[{NdefRecord.createMime(DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, stream.toByteArray())});} catch (IOException e) { }
這個(gè)片段在里面
public NdefMessage createNdefMessage(NfcEvent event)
你可以找到一個(gè)模板here
如果可能,我也想知道如何在配置的應(yīng)用程序啟動(dòng)后立即檢索該字符串值.
解決方法:
下面的代碼應(yīng)該是您正在尋找的.為簡(jiǎn)潔起見(jiàn),我只設(shè)置包名稱(chēng)加上兩個(gè)將發(fā)送到DeviceAdminReceiver的字符串.
@Overridepublic NdefMessage createNdefMessage(NfcEvent event) { try { Properties p = new Properties(); p.setProperty( DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, "com.example.some.app"); Properties extras = new Properties(); extras.setProperty("Key1", "TestString1"); extras.setProperty("Key2", "TestString2"); StringWriter sw = new StringWriter(); try{ extras.store(sw, "admin extras bundle"); p.put(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, sw.toString()); Log.d(TAG, "Admin extras bundle=" p.get( DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE)); } catch (IOException e) { Log.e(TAG, "Unable to build admin extras bundle"); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStream out = new ObjectOutputStream(bos); p.store(out, ""); final byte[] bytes = bos.toByteArray(); NdefMessage msg = new NdefMessage(NdefRecord.createMime( DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, bytes)); return msg; } catch (Exception e) { throw new RuntimeException(e); }}
下一個(gè)片段將進(jìn)入您的DeviceAdminReceiver,以便接收“Admin Extras”…如果您不覆蓋onReceive,則需要覆蓋onProfileProvisioningComplete并在其中處理EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE.
@Overridepublic void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive " intent.getAction()); if (ACTION_PROFILE_PROVISIONING_COMPLETE.equals(intent.getAction())) { PersistableBundle extras = intent.getParcelableExtra( EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE); Log.d(TAG, "onReceive Extras:" extras.getString("Key1") " / " extras.getString("Key2")); }}
來(lái)源:https://www.icode9.com/content-1-360851.html
聯(lián)系客服