安卓NFC设备所有者配置:发送自定义属性。这是否可能?

3
我正在开发一款应用程序,并遇到以下问题。
在使用NFC设备所有者配置时,我希望发送一个字符串,该字符串将被新设备所有者应用程序使用。

我知道设备所有者配置的标准MIME属性,可以在这里找到。

这里有一段代码片段可以更好地展示我的问题。请注意 "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) {                         

}

这个片段位于内部

public NdefMessage createNdefMessage(NfcEvent event)

你可以在这里找到一个模板 here

如果可能的话,我还想知道如何在提供的应用程序启动后尽快检索该字符串值。

2个回答

7
下面的代码应该是你要找的。为了简洁起见,我只设置了包名和将发送到你的DeviceAdminReceiver的两个字符串。
@Override
public 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);
    }
}

下面的代码片段将被放置在您的DeviceAdminReceiver中,以接收“管理者额外信息”... 如果您不覆盖onReceive,则需要覆盖onProfileProvisioningComplete并在其中处理EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE。

@Override
public 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"));
    }
}

0

onProfileProvisioningComplete 将被覆盖为以下内容。

@Override
public void onProfileProvisioningComplete(final Context context, final Intent intent)
{
    final PersistableBundle extras = intent.getParcelableExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
    Log.d(TAG, "onProfileProvisioningComplete Extras:" + extras.getString("Key1") + " / " + extras.getString("Key2"));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接