获取标签中的onCreate

获取标签中的onCreate

本文介绍了安卓/ NFC:获取标签中的onCreate()没有新意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我工作的NFC应用。要,我使用的是NDEF标签与AAR NDEF记录里开始我的应用程序。

I am working on an NFC-application.To start my app, I am using a NDEF-tag with an AAR NDEF Record inside.

这工作得很好。但现在我想读取标签的内容直接与应用程序。我怎样才能做到这一点?

This works fine. But now I want to read the tag content with the app directly.How can I do this?

(它已经工作,当我删除标签从手机,再触摸它,但我想要消除此步骤。)

(It already works, when I remove the tag from the phone and touch it again, but I want to eliminate this step.)

更新:更多的细节好吧,使其更清晰,这里是我目前的code某些部分。

Update: Some more detailsOk, to make it more clear, here are some parts of my current code.

private NfcAdapter nfcAdapter;
private static final int PENDING_INTENT_TECH_DISCOVERED = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_l);
    text_output = (TextView) findViewById(R.id.textView2);

    NfcAdapter adapter = ((NfcManager) getSystemService(Context.NFC_SERVICE))
            .getDefaultAdapter();
}

@Override
public void onResume() {
    super.onResume();

    nfcAdapter = ((NfcManager) this.getSystemService(Context.NFC_SERVICE))
            .getDefaultAdapter();

    PendingIntent pi = createPendingResult(PENDING_INTENT_TECH_DISCOVERED,
            new Intent(), 0);

    nfcAdapter.enableForegroundDispatch(this, pi,
            new IntentFilter[] { new IntentFilter(
                    NfcAdapter.ACTION_TECH_DISCOVERED) }, new String[][] {
                    new String[] { "android.nfc.tech.NdefFormatable" },
                    new String[] { "android.nfc.tech.Ndef" } });
}

@Override
public void onPause() {
    super.onPause();
    nfcAdapter.disableForegroundDispatch(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        final Intent data) {
    switch (requestCode) {
    case PENDING_INTENT_TECH_DISCOVERED:
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                doTagOperation(data);
            }
        };
        new Thread(runnable).start();

        break;
    }
}

private void doTagOperation(Intent data) {

    try {
        String action = data.getAction();
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

            Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            Ndef ndefTag = Ndef.get(tag);
        }
    } catch (Exception ex) {
                ...
    }
}

要启动的应用程序,我的标签,它可以以这种方式创建使用AAR-NDEF-实录:    NdefRecord NDR = NdefRecord.createApplicationRecord(com.example.something);

To start the app I use an AAR-NDEF-Record on the tag, which can be created in this way: NdefRecord ndr = NdefRecord.createApplicationRecord("com.example.something");

所以,我要的是:触摸标签时,应用程序未启动,使应用程序开始(工作的话),然后它应该直接读取标签,而无需再次移动手机远离标签和触摸它的需要。

So what I want is: Touch the tag when the app is not started makes the app starting (works already) and then it should directly read the tag without the need of moving the phone away from the tag and touch it again.

推荐答案

如果你想在你的活动开始接收NDEF发现的意图,你需要添加一个意图过滤器对标签的NDEF消息触发您的应用程序清单:

If you want to receive the NDEF discovery intent upon the start of your activity, you need to add an intent filter that triggers on the tag's NDEF message to your application manifest:

<activity ... >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

我想你已经有了上面的,所以你需要添加一个NDEF意图过滤器:

I guess you already have the above, so you will need to add an NDEF intent filter:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />

您可以使用一个基于URI触发:

You can use either a URI based trigger:

        <data android:scheme="http" android:host="mroland.at" android:pathPrefix="/test" />

或MIME基于方案:

Or a MIME based scheme:

        <data android:mimeType="application/vnd.mroland.test" />

或者,如果你想获得NDEF发现意图只包含一个AAR标签(我必须承认,我从来没有测试过这个,所以我只能认为这应该工作):

Or if you want to receive the NDEF discovered intent for a tag that contains only an AAR (I have to admit that I never tested this so I only assume that this should work):

        <data android:scheme="vnd.android.nfc" android:host="ext"
              android:pathPrefix="/android.com:pkg" />

但你要使用其中一个。 (至少在大多数设备,你不得不这样做。有些设备/ Android的版本将接受意图过滤器,即使没有数据元素。)

    </intent-filter>
</activity>

您可以再拿到的意图的onCreate() ONSTART() onResume()或你想要使用 getIntent()方法读取它。

You can then get the intent in onCreate(), onStart(), onResume() or wherever you want to read it using the getIntent() method.

请注意,如果您没有为 NDEF_DISCOVERED 添加一个意图过滤器到你的清单中,NDEF消息与AAR为您的应用将导致意图动作与类别 LAUNCHER 来传递给你的第一的活动,声明这样一个意图过滤器。在这种情况下,您将不会收到NDEF消息。所以,如果您要接收NDEF消息必须以声明的意图过滤器的 NDEF_DISCOVERED 。在这种情况下,您可以选择的活动开始由AAR通过设置 NDEF_DISCOVERED 意图过滤器为特定的活动。

Note that if you don't add an intent filter for NDEF_DISCOVERED to your manifest, an NDEF message with an AAR for your application will cause the intent action MAIN with the category LAUNCHER to be passed to your first activity that declares such an intent filter. In that case you will not receive the NDEF message. So if you want to receive the NDEF message you have to to declare an intent filter for NDEF_DISCOVERED. In that case, you can choose which activity is started by the AAR by setting the NDEF_DISCOVERED intent filter for that specific activity.

这篇关于安卓/ NFC:获取标签中的onCreate()没有新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:32