zoukankan      html  css  js  c++  java
  • 7、NFC技术:让Android自动运行程序

    用于描述NDEF格式数据的两个重要的类

    NdefMessage:描述NDEF格式的信息
    NdefRecord:描述NDEF信息的一个信息段 

          NdefMessage和NdefRecord是Android NFC技术的核心类,无论读写NDEF格式的NFC标签,还是通过Android Beam技术传递Ndef格式的数据,都需要这两个类。

     
    向NFC标签写入数据的步骤
    获取Tag对象

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    判断NFC标签的数据类型(通过Ndef.get方法)

    Ndef ndef = Ndef.get(tag);

    写入数据

    ndef.writeNdefMessage(ndefMessage);

    示例:自动启动Android应用程序
          
     最终效果:写入后,关闭程序,回到主页面,然后拿NFC标签靠近手机后盖上部,程序回自动运行。  实现代码如下所示。
     1 import java.util.ArrayList;
     2 import java.util.List;
     3 
     4 import android.app.ListActivity;
     5 import android.content.Intent;
     6 import android.content.pm.PackageInfo;
     7 import android.content.pm.PackageManager;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 import android.widget.AdapterView;
    11 import android.widget.AdapterView.OnItemClickListener;
    12 import android.widget.ArrayAdapter;
    13 
    14 /**
    15  * LIST列表,显示所有的包。
    16  * @author dr
    17  */
    18 public class InstalledApplicationListActivity extends ListActivity implements
    19         OnItemClickListener {
    20     
    21     private List<String> mPackages = new ArrayList<String>();
    22 
    23     @Override
    24     public void onCreate(Bundle savedInstanceState) {
    25         super.onCreate(savedInstanceState);
    26 
    27         // 获取所有程序包名称,并且循环显示出来。
    28         PackageManager packageManager = getPackageManager();
    29         List<PackageInfo> packageInfos = packageManager
    30                 .getInstalledPackages(PackageManager.GET_ACTIVITIES);
    31         for (PackageInfo packageInfo : packageInfos) {
    32             mPackages.add(packageInfo.applicationInfo.loadLabel(packageManager)
    33                     + "
    " + packageInfo.packageName);
    34         }
    35 
    36         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
    37                 android.R.layout.simple_list_item_1, android.R.id.text1,
    38                 mPackages);
    39         setListAdapter(arrayAdapter);
    40         // 列表项,单击事件。
    41         getListView().setOnItemClickListener(this);
    42     }
    43 
    44     @Override
    45     public void onItemClick(AdapterView<?> parent, View view, int position,
    46             long id) {
    47         Intent intent = new Intent();
    48         // 把选中的包名传过去
    49         intent.putExtra("package_name", mPackages.get(position));
    50         setResult(1, intent);
    51         finish();
    52 
    53     }
    54 
    55 }
      1 mport android.app.Activity;
      2 import android.app.PendingIntent;
      3 import android.content.Intent;
      4 import android.nfc.NdefMessage;
      5 import android.nfc.NdefRecord;
      6 import android.nfc.NfcAdapter;
      7 import android.nfc.Tag;
      8 import android.nfc.tech.Ndef;
      9 import android.os.Bundle;
     10 import android.view.View;
     11 import android.widget.Button;
     12 import android.widget.Toast;
     13 
     14 public class RunApplicationActivity extends Activity {
     15 
     16     private Button mSelectAutoRunApplication;
     17     private String mPackageName;
     18     private NfcAdapter mNfcAdapter;
     19     private PendingIntent mPendingIntent;
     20 
     21     @Override
     22     protected void onCreate(Bundle savedInstanceState) {
     23         // TODO Auto-generated method stub
     24         super.onCreate(savedInstanceState);
     25         setContentView(R.layout.activity_auto_run_application);
     26 
     27         mSelectAutoRunApplication = (Button) findViewById(R.id.button_select_auto_run_application);
     28 
     29         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
     30         // 一旦截获NFC消息后,通过PendingIntent来调用
     31         mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
     32                 getClass()), 0);
     33 
     34     }
     35 
     36     public void onResume() { // 当窗口获得焦点时
     37         super.onResume();
     38 
     39         if (mNfcAdapter != null)
     40             // (一旦截获NFC消息)优先级,优于所有的(处理NFC标签)窗口
     41             mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
     42                     null);
     43     }
     44 
     45     public void onPause() { //
     46         super.onPause();
     47 
     48         if (mNfcAdapter != null)
     49             // 取消,把窗口恢复到正常状态。
     50             mNfcAdapter.disableForegroundDispatch(this);
     51     }
     52 
     53     public void onClick_SelectAutoRunApplication(View view) {
     54         Intent intent = new Intent(this, InstalledApplicationListActivity.class);
     55         startActivityForResult(intent, 0);
     56     }
     57 
     58     /**
     59      * 因为此Activity配置配件中设置成singleTop(第2次运行onCreate将不会创建新的窗口实例),
     60      * 不能在onCreate中获取Intent传过来的TAG数据。 但是,会调用此方法,onNewIntent也是Activity里面的方法。
     61      */
     62     public void onNewIntent(Intent intent) {
     63         if (mPackageName == null)
     64             return;
     65         // 获得Tag。
     66         Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
     67         // 写入标签。
     68         writeNFCTag(detectedTag);
     69     }
     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             // 判断NFC标签的数据类型。
     81             Ndef ndef = Ndef.get(tag);
     82             if (ndef != null) {
     83                 ndef.connect(); // 建立连接
     84 
     85                 if (!ndef.isWritable()) { // 判断NFC标签是否可写。
     86                     return;
     87                 }
     88                 if (ndef.getMaxSize() < size) { // 最大尺寸<写入尺寸。
     89                     return;
     90                 }
     91                 // 写入数据。
     92                 ndef.writeNdefMessage(ndefMessage);
     93                 Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
     94             }
     95 
     96         } catch (Exception e) {
     97             // TODO: handle exception
     98         }
     99     }
    100 
    101     @Override
    102     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    103         if (resultCode == 1) {
    104             mSelectAutoRunApplication.setText(data.getExtras().getString(
    105                     "package_name"));
    106             String temp = mSelectAutoRunApplication.getText().toString();
    107             mPackageName = temp.substring(temp.indexOf("
    ") + 1);
    108 
    109         }
    110 
    111     }
    112 
    113 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <Button
     8         android:id="@+id/button_select_auto_run_application"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:onClick="onClick_SelectAutoRunApplication"
    12         android:text="选择已安装的应用程序" />
    13 
    14     <TextView
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:layout_marginBottom="5dp"
    18         android:text="请将NFC标签或贴纸靠近手机背面"
    19         android:textSize="16sp" />
    20 
    21     <ImageView
    22         android:layout_width="match_parent"
    23         android:layout_height="match_parent"
    24         android:layout_margin="10dp"
    25         android:src="@drawable/read_nfc_tag" />
    26 
    27 </LinearLayout>
     1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2     package="cn.eoe.run.application"
     3     android:versionCode="1"
     4     android:versionName="1.0" >
     5 
     6     <uses-sdk
     7         android:minSdkVersion="15"
     8         android:targetSdkVersion="15" />
     9 
    10     <uses-permission android:name="android.permission.NFC" />
    11 
    12     <application
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             android:name=".RunApplicationActivity"
    18             android:label="@string/title_activity_auto_run_application"
    19             android:launchMode="singleTop"
    20             android:screenOrientation="portrait" >
    21             <intent-filter>
    22                 <action android:name="android.intent.action.MAIN" />
    23 
    24                 <category android:name="android.intent.category.LAUNCHER" />
    25             </intent-filter>
    26         </activity>
    27         <activity
    28             android:name=".InstalledApplicationListActivity"
    29             android:label="@string/title_activity_installed_application_list"
    30             android:screenOrientation="portrait" />
    31 
    32     </application>
    33 
    34 </manifest>
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    delete 用法总结
    js数组去重的常用方法总结
    学习中 常用到的string内置对象方法的总结
    Array 对象常用的方法总结
    javascript中运算符有哪些? 他们的优先级 呢?
    那些年前端经典面试题
    HHVM 3.0 发布,执行 PHP 的虚拟机
    【问底】徐汉彬:PHP7和HHVM的性能之争 (真是学到了很多)
    mysql 简单sql语句
    【问底】王帅:深入PHP内核(一)——弱类型变量原理探究
  • 原文地址:https://www.cnblogs.com/androidsj/p/3856421.html
Copyright © 2011-2022 走看看