NFC的Andr​​oid应用程序 [英] Android Application with NFC

查看:138
本文介绍了NFC的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个Android应用程序中,我需要有一个执行以下活动: 它加载了,然后等待,直到一个NF​​C标签被检测到。我真的不关心标签被解析的方式(无论是智能海报或URI等)。我感兴趣的唯一的事情是,标签的ID。 一旦标签和它的ID被检测到,我想执行一些计算,然后返回到等待状态(其中应用程序正在等待检测NFC标签的状态)。

I am trying to build an Android application in which I need to have an activity that does the following: It loads up and then waits until a NFC tag is detected. I do not really care about the way the tag is parsed (whether it is a smart poster or URI etc..). The only thing i am interested in is the ID of that tag. Once a tag and its ID are detected, I want to perform some computations, and then go back to the waiting state (the state where the application is waiting to detect an NFC tag).

我的问题是,我无法弄清楚如何使我的code。通过标记的检测被触发。 (请注意,该应用程序正在运行,所以它不是应用程序优先级的问题。相反,我想我的code被标记的检测来触发,然后返回到等待状态)。

My problem is that I cannot figure out how to make all my code be triggered by the detection of a tag. (Please note that the application is running so it is not a problem of application priority. Instead, I want my code to be triggered by the detection of a tag, and then go back to waiting state).

非常感谢你。

推荐答案

在这里,我们走了,低于code。诀窍是登记前台标签调度使您的活动获得所有新标签。同时指定标志SINGLE_TOP这样一个实例活跃的活动将被称为onNewIntent。将张贴ForegroundUtil了。

Here we go, code below. The trick is to register the foreground tag dispatch so your activity gets all the new tags. Also specify the flag SINGLE_TOP so the one instance active of the activity will be called with onNewIntent. Will post the ForegroundUtil, too.

public class DashboardActivity extends Activity {

NFCForegroundUtil nfcForegroundUtil = null;

private TextView info;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    info = (TextView)findViewById(R.id.info);

    nfcForegroundUtil = new NFCForegroundUtil(this);


}

public void onPause() {
    super.onPause();
    nfcForegroundUtil.disableForeground();
}   

public void onResume() {
    super.onResume();
    nfcForegroundUtil.enableForeground();

    if (!nfcForegroundUtil.getNfc().isEnabled())
    {
        Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }

}

public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    info.setText(NFCUtil.printTagDetails(tag));    

}


}

前景-的Util(您shoudl修改意图过滤器,以满足您的需求)

Foreground-Util (you shoudl modify the intent filter to fit your needs)

public class NFCForegroundUtil {

private NfcAdapter nfc;


private Activity activity;
private IntentFilter intentFiltersArray[];
private PendingIntent intent;
private String techListsArray[][];

public NFCForegroundUtil(Activity activity) {
    super();
    this.activity = activity; 
    nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());

    intent = PendingIntent.getActivity(activity, 0, new Intent(activity,
            activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Unable to speciy */* Mime Type", e);
    }
    intentFiltersArray = new IntentFilter[] { ndef };

    techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
    //techListsArray = new String[][] { new String[] { NfcA.class.getName(), NfcB.class.getName() }, new String[] {NfcV.class.getName()} };
}

public void enableForeground()
{
    Log.d("demo", "Foreground NFC dispatch enabled");
    nfc.enableForegroundDispatch(activity, intent, intentFiltersArray, techListsArray);     
}

public void disableForeground()
{
    Log.d("demo", "Foreground NFC dispatch disabled");
    nfc.disableForegroundDispatch(activity);
}

public NfcAdapter getNfc() {
    return nfc;
}   

}

这篇关于NFC的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆