使用NFC让Android程序自动运行

本文是使用了NFC的标签纸和小米3手机测试

首先新建一个Activity类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.cayden.run.application;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.PendingIntent;  
  5. import android.content.Intent;  
  6. import android.nfc.NdefMessage;  
  7. import android.nfc.NdefRecord;  
  8. import android.nfc.NfcAdapter;  
  9. import android.nfc.Tag;  
  10. import android.nfc.tech.Ndef;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15. /** 
  16.  *  
  17.  *   
  18.  * @author cuiran 
  19.  * @version 1.0.0 
  20.  */  
  21. public class RunApplicationActivity extends Activity {  
  22.   
  23.     private Button mSelectAutoRunApplication;  
  24.     private String mPackageName;  
  25.     private NfcAdapter mNfcAdapter;  
  26.     private PendingIntent mPendingIntent;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         // TODO Auto-generated method stub  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.activity_auto_run_application);  
  33.   
  34.         mSelectAutoRunApplication = (Button) findViewById(R.id.button_select_auto_run_application);  
  35.   
  36.         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);  
  37.         mPendingIntent = PendingIntent.getActivity(this0new Intent(this,  
  38.                 getClass()), 0);  
  39.   
  40.     }  
  41.   
  42.     public void onNewIntent(Intent intent) {  
  43.         if (mPackageName == null)  
  44.             return;  
  45.   
  46.         Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  47.   
  48.         writeNFCTag(detectedTag);  
  49.     }  
  50.   
  51.     public void onResume() {  
  52.         super.onResume();  
  53.   
  54.         if (mNfcAdapter != null)  
  55.             mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,  
  56.                     null);  
  57.   
  58.     }  
  59.   
  60.     public void onPause() {  
  61.         super.onPause();  
  62.   
  63.         if (mNfcAdapter != null)  
  64.             mNfcAdapter.disableForegroundDispatch(this);  
  65.     }  
  66.     public void onClick_SelectAutoRunApplication(View view)  
  67.     {  
  68.         Intent intent = new Intent(this, InstalledApplicationListActivity.class);  
  69.         startActivityForResult(intent, 0);  
  70.     }  
  71.     public void writeNFCTag(Tag tag) {  
  72.         if (tag == null) {  
  73.             return;  
  74.         }  
  75.         NdefMessage ndefMessage = new NdefMessage(  
  76.                 new NdefRecord[] { NdefRecord  
  77.                         .createApplicationRecord(mPackageName) });  
  78.         int size = ndefMessage.toByteArray().length;  
  79.         try {  
  80.             Ndef ndef = Ndef.get(tag);  
  81.             if(ndef!=null)  
  82.             {  
  83.                 ndef.connect();  
  84.                   
  85.                 if(!ndef.isWritable())  
  86.                 {  
  87.                     return;  
  88.                 }  
  89.                 if(ndef.getMaxSize() < size)  
  90.                 {  
  91.                     return;  
  92.                 }  
  93.                 ndef.writeNdefMessage(ndefMessage);  
  94.                 Toast.makeText(this"ok", Toast.LENGTH_LONG).show();  
  95.             }  
  96.   
  97.         } catch (Exception e) {  
  98.             // TODO: handle exception  
  99.         }  
  100.     }  
  101.     @Override  
  102.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
  103.     {  
  104.         if (resultCode == 1)  
  105.         {  
  106.             mSelectAutoRunApplication.setText(data.getExtras().getString(  
  107.                     "package_name"));  
  108.             String temp = mSelectAutoRunApplication.getText().toString();  
  109.             mPackageName = temp.substring(temp.indexOf("\n") + 1);  
  110.               
  111.         }  
  112.   
  113.     }  
  114.   
  115.   
  116.   
  117.   
  118. }  

在此Activity中,可以点击按钮然后跳转到另一个Activity,是用来显示应用列表

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.cayden.run.application;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.ListActivity;  
  7. import android.content.Intent;  
  8. import android.content.pm.PackageInfo;  
  9. import android.content.pm.PackageManager;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.ArrayAdapter;  
  15.   
  16. public class InstalledApplicationListActivity extends ListActivity implements  
  17.         OnItemClickListener {  
  18.     private List<String> mPackages = new ArrayList<String>();  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.   
  24.         PackageManager packageManager = getPackageManager();  
  25.         List<PackageInfo> packageInfos = packageManager  
  26.                 .getInstalledPackages(PackageManager.GET_ACTIVITIES);  
  27.         for (PackageInfo packageInfo : packageInfos) {  
  28.   
  29.             mPackages.add(packageInfo.applicationInfo.loadLabel(packageManager)  
  30.                     + "\n" + packageInfo.packageName);  
  31.         }  
  32.   
  33.         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,  
  34.                 android.R.layout.simple_list_item_1, android.R.id.text1,  
  35.                 mPackages);  
  36.         setListAdapter(arrayAdapter);  
  37.         getListView().setOnItemClickListener(this);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  42.             long id) {  
  43.         Intent intent = new Intent();  
  44.         intent.putExtra("package_name", mPackages.get(position));  
  45.         setResult(1, intent);  
  46.         finish();  
  47.   
  48.     }  
  49.   
  50. }  

当选择应用后,会回调并传回

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. packageInfo.applicationInfo.loadLabel(packageManager)  
  2.                     + "\n" + packageInfo.packageName  
具体操作,先将nfc标签纸放入手机背面会执行

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void onNewIntent(Intent intent) {  
  2.         if (mPackageName == null)  
  3.             return;  
  4.   
  5.         Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  6.   
  7.         writeNFCTag(detectedTag);  
  8.     }  
如果不为空,就将所选择的应用写入标签纸

然后退出应用后,将标签纸放入手机背面就会自动执行所选择的应用程序。

关于NdefRecord 在官网也给出了api

http://developer.android.com/reference/android/nfc/package-summary.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值