1 //实现向NFC标签写入包,然后将NFC靠近手机背面摄像头位置,自动运行写入的程序 2 public class RunAppActivity extends Activity { 3 private Button button; 4 private String mpackage; 5 private NfcAdapter nfcAdapter; 6 private PendingIntent mPendingIntent; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 // TODO Auto-generated method stub 11 super.onCreate(savedInstanceState); 12 13 setContentView(R.layout.fragment_main); 14 button = (Button) findViewById(R.id.button1); 15 nfcAdapter = NfcAdapter.getDefaultAdapter(this); 16 mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 17 getClass()), 0); 18 19 } 20 21 @Override 22 protected void onNewIntent(Intent intent) { 23 // TODO Auto-generated method stub 24 // 当清单文件中设置为"singleTop"时,oncreate()只调用一次,而onNewIntent()每次调用 25 super.onNewIntent(intent); 26 if (mpackage == null) 27 return; 28 // 第一步获得Tag 29 Tag detectetTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 30 // 第二步写入标签 31 writeNFCTag(detectetTag); 32 } 33 34 private void writeNFCTag(Tag tag) { 35 // TODO Auto-generated method stub 36 if (tag == null) { 37 return; 38 } 39 40 NdefMessage ndefMessage = new NdefMessage( 41 new NdefRecord[] { NdefRecord.createApplicationRecord(mpackage) }); 42 43 int size = ndefMessage.toByteArray().length; 44 try { 45 Ndef ndef = Ndef.get(tag); 46 if (ndef != null) { 47 ndef.connect(); 48 // 是否支持可写 49 if (!ndef.isWritable()) { 50 return; 51 } 52 // 判断是否可以容纳要写入的数据 53 if (ndef.getMaxSize() < size) { 54 return; 55 } 56 ndef.writeNdefMessage(ndefMessage); 57 Toast.makeText(this, "NFC成功写入", 0).show(); 58 } 59 } catch (Exception e) { 60 // TODO: handle exception 61 } 62 } 63 64 @Override 65 protected void onResume() { 66 // TODO Auto-generated method stub 67 super.onResume(); 68 // 获得焦点, 设置这个窗口优先级高于所有可以处理NFC的窗口 69 if (nfcAdapter != null) { 70 nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, 71 null); 72 } 73 } 74 75 @Override 76 protected void onPause() { 77 // TODO Auto-generated method stub 78 super.onPause(); 79 // 程序退出,窗口恢复默认 80 if (nfcAdapter != null) { 81 nfcAdapter.disableForegroundDispatch(this); 82 } 83 } 84 85 public void onclick(View view) { 86 Intent intent = new Intent(this, InstallAppListActivity.class); 87 startActivityForResult(intent, 0); 88 } 89 }
1 //显示所有安装的包 2 public class InstallAppListActivity extends ListActivity implements 3 OnItemClickListener { 4 private List<String> mpackages = new ArrayList<String>(); 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 super.onCreate(savedInstanceState); 10 PackageManager manager = getPackageManager(); 11 List<PackageInfo> packageInfos = manager 12 .getInstalledPackages(PackageManager.GET_ACTIVITIES); 13 14 for (PackageInfo packageInfo : packageInfos) { 15 mpackages.add(packageInfo.applicationInfo.loadLabel(manager) + "\n" 16 + packageInfo.packageName); 17 18 } 19 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 20 android.R.layout.simple_list_item_1, android.R.id.text1, 21 mpackages); 22 setListAdapter(adapter); 23 getListView().setOnItemClickListener(this); 24 } 25 26 @Override 27 public void onItemClick(AdapterView<?> parent, View view, int position, 28 long id) { 29 Intent intent = new Intent(); 30 intent.putExtra("Packagename", mpackages.get(position)); 31 setResult(1, intent); 32 finish(); 33 } 34 35 }