0

主な問題は、他の NFC 対応電話 (NFC がオン) で電話をタップすると、onNewIntent() に移動できないことです。主な意図以外の状況では、onNewIntent に到達できません。NDEF、TECH、TAGの3つのフィルターすべてを試しました。

package com.example.nfctry;

import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context ctx;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ctx = this;

    adapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    writeTagFilters = new IntentFilter[] {tagDetected};

    onNewIntent(getIntent());



}

@Override
protected void onNewIntent(Intent intent)
{
    Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
    super.onNewIntent(intent);
    // getIntent() should always return the most recent
    if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
    {
        myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Toast.makeText(this,"DETECTED muahhhhh"  + myTag.toString(), Toast.LENGTH_LONG).show();

    }


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Android mainfest では、intentfilters も追加しました。

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfctry"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nfctry.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />

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

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

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

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
        <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />
    </activity>
</application>

4

1 に答える 1

3

どの TAG フィルターを使用するかに依存しません。:を使用してonNewIntentan が呼び出されたときにのみ get が呼び出されます。もちろん、自分で呼び出したときにも呼び出されます。アプリケーションが前面にある場合、NDEF/TECH/TAG の検出をキャッチしません。現在のアプリでタグ検出イベントをキャッチするために使用する必要があります。intentlaunchModesingleTopsingleTaskForegroundDispatching

イベントをForegroundDispatchキャッチPendingIntent.getActivityしてフラグを使用すると、が呼び出さFLAG_ACTIVITY_SINGLE_TOPれます。PendingIntentonNewIntent

ForegroundDispatchinを有効にする必要がありますonResume:

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

    PendingIntent pendingIntent     = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);     
    IntentFilter[] intentFilters    = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };      

    adapter.enableForegroundDispatch(   this,
            pendingIntent, 
            intentFilters,
            new String[][]{
            new String[]{"android.nfc.tech.NfcA"}
        });     
}

そしてそれを無効にしonPauseます:

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

    if (adapter != null) 
    {
        try {
            adapter.disableForegroundDispatch(this);
        } 
        catch (NullPointerException e) {
        }
    }
}  

現在のコードで TAG イベントをキャッチするには、次のForegroundDispatchようにします。

public class MainActivity extends Activity {

    NfcAdapter adapter;
    PendingIntent pendingIntent;
    IntentFilter writeTagFilters[];
    boolean writeMode;
    Tag myTag;
    Context ctx;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = this;
        adapter = NfcAdapter.getDefaultAdapter(this);
        onNewIntent(this.getIntent());      
    }


    @Override
    public void onNewIntent(Intent data) 
    {
        Toast.makeText(this,""+intent.getAction(), Toast.LENGTH_LONG).show();
        super.onNewIntent(intent);
        // getIntent() should always return the most recent
        if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
        {
            myTag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            Toast.makeText(this,"DETECTED muahhhhh"  + myTag.toString(), Toast.LENGTH_LONG).show();

        }
    }   

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

        PendingIntent pendingIntent     = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);     
        IntentFilter[] intentFilters    = { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };      

        adapter.enableForegroundDispatch(   this,
                pendingIntent, 
                intentFilters,
                new String[][]{
                new String[]{"android.nfc.tech.NfcA"}
            });     
    }

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

        if (adapter != null) 
        {
            try {
                adapter.disableForegroundDispatch(this);
            } 
            catch (NullPointerException e) {
            }
        }
    }    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

また、マニフェストに NFC ユーザー権限を追加する必要があることにも注意してください。

于 2014-08-23T09:05:05.077 に答える