当前位置:   article > 正文

make a vcard/vcal Ndef message on Android_text/x-vcard

text/x-vcard
  1. NfcAdapter adapter; 
  2. PendingIntent pendingIntent; 
  3. IntentFilter writeTagFilters[]; 
  4. Tag tag; 
  5. Context ctx; 
  6. boolean writeMode; 
  7.  
  8. @Override 
  9. public void onCreate(Bundle savedInstanceState) { 
  10.     super.onCreate(savedInstanceState); 
  11.     setContentView(R.layout.activity_text); 
  12.     getActionBar().setDisplayHomeAsUpEnabled(true); 
  13.  
  14.     ctx = this
  15.     Switch swWrite = (Switch) findViewById(R.id.switchWriteText); 
  16.     //final TextView msg = (TextView) findViewById(R.id.editText1); 
  17.     final String msg = "BEGIN:VCARD\n"
  18.             "VERSION:2.1\n"
  19.             "N:Gump;Forrest\n"
  20.             "FN:Forrest Gump\n"
  21.             "ORG:Bubba Gump Shrimp Co.\n"
  22.             "TITLE:Shrimp Man\n"
  23.             "TEL;WORK;VOICE111) 555-1212\n"
  24.             "TEL;HOME;VOICE404) 555-1212\n"
  25.             "ADR;WORK:;;100 Edge;Baytown;United\n"
  26.             "EMAIL;PREF;INTERNET:forrestgump@example.com\n "
  27.             "END:VCARD"
  28.     swWrite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
  29.  
  30.         @Override 
  31.         public void onCheckedChanged(CompoundButton buttonView, 
  32.                 boolean isChecked) { 
  33.             if (isChecked) { 
  34.                 try
  35.                     if (tag == null) { 
  36.                         Toast.makeText(ctx, 
  37.                                 ctx.getString(R.string.error_detected), 
  38.                                 Toast.LENGTH_SHORT).show(); 
  39.                     } else
  40.                         write(msg, tag); 
  41.                         Toast.makeText(ctx, 
  42.                                 ctx.getString(R.string.ok_writing), 
  43.                                 Toast.LENGTH_LONG).show(); 
  44.                     } 
  45.                 } catch (IOException e) { 
  46.                     Toast.makeText(ctx, 
  47.                             ctx.getString(R.string.error_writing), 
  48.                             Toast.LENGTH_LONG).show(); 
  49.                     e.printStackTrace(); 
  50.                 } catch (FormatException e) { 
  51.                     Toast.makeText(ctx, 
  52.                             ctx.getString(R.string.error_writing), 
  53.                             Toast.LENGTH_LONG).show(); 
  54.                     e.printStackTrace(); 
  55.                 } 
  56.  
  57.             } 
  58.         } 
  59.     }); 
  60.  
  61.     /* INTENT FILTER */ 
  62.     adapter = NfcAdapter.getDefaultAdapter(this); 
  63.     pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this
  64.             getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
  65.     IntentFilter tagDetected = new IntentFilter( 
  66.             NfcAdapter.ACTION_TAG_DISCOVERED); 
  67.     tagDetected.addCategory(Intent.CATEGORY_DEFAULT); 
  68.     writeTagFilters = new IntentFilter[] { tagDetected }; 
  69.  
  70. @Override 
  71. public boolean onCreateOptionsMenu(Menu menu) { 
  72.     getMenuInflater().inflate(R.menu.activity_text, menu); 
  73.     return true
  74.  
  75. @Override 
  76. public boolean onOptionsItemSelected(MenuItem item) { 
  77.     switch (item.getItemId()) { 
  78.     case android.R.id.home: 
  79.         NavUtils.navigateUpFromSameTask(this); 
  80.         return true
  81.     } 
  82.     return super.onOptionsItemSelected(item); 
  83.  
  84. private void write(String text, Tag tag) throws IOException, 
  85.         FormatException { 
  86.  
  87.     NdefRecord[] records = { createRecord(text) }; 
  88.     NdefMessage message = new NdefMessage(records); 
  89.     // Get an instance of Ndef for the tag. 
  90.     Ndef ndef = Ndef.get(tag); 
  91.     // Enable I/O 
  92.     ndef.connect(); 
  93.     // Write the message 
  94.     ndef.writeNdefMessage(message); 
  95.     // Close the connection 
  96.     ndef.close(); 
  97.  
  98. private NdefRecord createRecord(String text) 
  99.         throws UnsupportedEncodingException { 
  100.     String msg = "BEGIN:VCARD\n"
  101.             "VERSION:2.1\n"
  102.             "N:Gump;Forrest\n"
  103.             "FN:Forrest Gump\n"
  104.             "ORG:Bubba Gump Shrimp Co.\n"
  105.             "TITLE:Shrimp Man\n"
  106.             "TEL;WORK;VOICE:55-1212\n"
  107.             "TEL;HOME;VOICE:55-1212\n"
  108.             "ADR;WORK:;;100 Edge;Ban;United\n"
  109.             "EMAIL;PREF;INTERNET:p@example.com\n "
  110.             "END:VCARD"
  111.  
  112.             byte[] textBytes = msg.getBytes(); 
  113.  
  114.  
  115.             NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, 
  116.             "text/x-vCard".getBytes(), new byte[] {}, textBytes); 
  117.  
  118.     return textRecord; 
  119.  
  120. @Override 
  121. protected void onNewIntent(Intent intent) { 
  122.     if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
  123.         tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
  124.         Toast.makeText(this
  125.                 this.getString(R.string.ok_detection) + tag.toString(), 
  126.                 Toast.LENGTH_LONG).show(); 
  127.     } 
  128.  
  129. @Override 
  130. public void onPause() { 
  131.     super.onPause(); 
  132.     WriteModeOff(); 
  133.  
  134. @Override 
  135. public void onResume() { 
  136.     super.onResume(); 
  137.     WriteModeOn(); 
  138.  
  139. private void WriteModeOn() { 
  140.     writeMode = true
  141.     adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, 
  142.             null); 
  143.  
  144. private void WriteModeOff() { 
  145.     writeMode = false
  146.     adapter.disableForegroundDispatch(this); 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/314864
推荐阅读
相关标签
  

闽ICP备14008679号