本文是使用了NFC的标签纸和小米3手机测试
首先新建一个Activity类
- package com.cayden.run.application;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.nfc.NdefMessage;
- import android.nfc.NdefRecord;
- import android.nfc.NfcAdapter;
- import android.nfc.Tag;
- import android.nfc.tech.Ndef;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- /**
- *
- *
- * @author cuiran
- * @version 1.0.0
- */
- public class RunApplicationActivity extends Activity {
- private Button mSelectAutoRunApplication;
- private String mPackageName;
- private NfcAdapter mNfcAdapter;
- private PendingIntent mPendingIntent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_auto_run_application);
- mSelectAutoRunApplication = (Button) findViewById(R.id.button_select_auto_run_application);
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
- mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
- getClass()), 0);
- }
- public void onNewIntent(Intent intent) {
- if (mPackageName == null)
- return;
- Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- writeNFCTag(detectedTag);
- }
- public void onResume() {
- super.onResume();
- if (mNfcAdapter != null)
- mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
- null);
- }
- public void onPause() {
- super.onPause();
- if (mNfcAdapter != null)
- mNfcAdapter.disableForegroundDispatch(this);
- }
- public void onClick_SelectAutoRunApplication(View view)
- {
- Intent intent = new Intent(this, InstalledApplicationListActivity.class);
- startActivityForResult(intent, 0);
- }
- public void writeNFCTag(Tag tag) {
- if (tag == null) {
- return;
- }
- NdefMessage ndefMessage = new NdefMessage(
- new NdefRecord[] { NdefRecord
- .createApplicationRecord(mPackageName) });
- int size = ndefMessage.toByteArray().length;
- try {
- Ndef ndef = Ndef.get(tag);
- if(ndef!=null)
- {
- ndef.connect();
- if(!ndef.isWritable())
- {
- return;
- }
- if(ndef.getMaxSize() < size)
- {
- return;
- }
- ndef.writeNdefMessage(ndefMessage);
- Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (resultCode == 1)
- {
- mSelectAutoRunApplication.setText(data.getExtras().getString(
- "package_name"));
- String temp = mSelectAutoRunApplication.getText().toString();
- mPackageName = temp.substring(temp.indexOf("\n") + 1);
- }
- }
- }
在此Activity中,可以点击按钮然后跳转到另一个Activity,是用来显示应用列表
- package com.cayden.run.application;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- public class InstalledApplicationListActivity extends ListActivity implements
- OnItemClickListener {
- private List<String> mPackages = new ArrayList<String>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- PackageManager packageManager = getPackageManager();
- List<PackageInfo> packageInfos = packageManager
- .getInstalledPackages(PackageManager.GET_ACTIVITIES);
- for (PackageInfo packageInfo : packageInfos) {
- mPackages.add(packageInfo.applicationInfo.loadLabel(packageManager)
- + "\n" + packageInfo.packageName);
- }
- ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, android.R.id.text1,
- mPackages);
- setListAdapter(arrayAdapter);
- getListView().setOnItemClickListener(this);
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- Intent intent = new Intent();
- intent.putExtra("package_name", mPackages.get(position));
- setResult(1, intent);
- finish();
- }
- }
当选择应用后,会回调并传回
- packageInfo.applicationInfo.loadLabel(packageManager)
- + "\n" + packageInfo.packageName
- public void onNewIntent(Intent intent) {
- if (mPackageName == null)
- return;
- Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- writeNFCTag(detectedTag);
- }
然后退出应用后,将标签纸放入手机背面就会自动执行所选择的应用程序。
关于NdefRecord 在官网也给出了api
http://developer.android.com/reference/android/nfc/package-summary.html