2016-06-25 10 views
2

私は現在、アプリを開発中で、次の問題があります。Android NFCデバイス所有者のプロビジョニング:カスタムプロパティを送信します。出来ますか?

デバイスオーナーのプロビジョニングにNFCを使用している間、新しいデバイスオーナーアプリで使用される文字列を送信したいと思います。

私は、デバイスの所有者プロビジョニングのための標準的なMIMEプロパティの承知しているが、here

を見つけたここに私の問題のより良いビジュアルを与えることができ抜粋です。 "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

を見つけることができ、私はまた、できるだけ早くその文字列値を取得する方法を知っていただきたいと思いますプロビジョニングされたアプリが起動しました。

ご回答ありがとうございます。

答えて

5

下記のコードはお探しのものです。簡潔にするために、私はあなたのDeviceAdminReceiverに送信される2つの文字列に加えてパッケージ名だけを設定します。

@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")); 
} 
関連する問題