2016-07-19 27 views
0

私はヒットNFCチップ、Iは、AndroidでNFCに関する次の情報を知っていれば、私はあなたがするNFCのアクションを登録することはできませんアンドロイドアプリケーションを特定のNFCチップに聴く方法を教えてください。

  1. 間違っている場合が私を修正してくださいいくつかのアクションを行うアプリケーションを作成していますマニフェストファイルの受信者。アクティビティのみ。
  2. 各NFCチップには独自のIDがあります。アプリケーションがバックグラウンドで動作しているか、私は私のアプリは、昼食と何らかのアクションを行うのID(例:1234)とNFCチップをヒットした場合、それは、閉じている間、私が何をしたいのか


です。

は可能ですか? はいの場合、どのように達成できますか?

編集 ここでは私のコードで、あなたが任意のNFCチップをチェックして、このアクションandroid.nfc.action.TECH_DISCOVERED を得るときには開きませんが、それは、アクションを開いたときandroid.intent.action.MAIN MainActivity.java

public class MainActivity extends AppCompatActivity { 
    public static final String ACTION_CHECK_IN= "checked"; 
    public static final String ACTION_CHECK_OUT= "unchecked"; 
    private NfcAdapter mNfcAdapter; 
    boolean isCheckedIn = false; 

    private static final String TAG = "MainActivity"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     isCheckedIn = Pref.getBoolean("check", false); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(TAG, "onResume() called with: " + ""); 
     String action = getIntent().getAction(); 
     if (action != null) { 
      Log.d("MainActivity", "onCreate(" + action + ")"); 
      if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) { 

       if (!isCheckedIn) { 
        isCheckedIn = true; 
        Pref.putBoolean("check", true); 
        Log.d("checking","IN"); 
       } else { 
        isCheckedIn = false; 
        Pref.putBoolean("check", false); 
        Log.d("checking","Out"); 
       } 

      } 

     } 

     if (!mNfcAdapter.isEnabled()) { 

      Toast.makeText(MainActivity.this, "Please open it ", Toast.LENGTH_SHORT).show(); 
     } 


     /** 
     * It's important, that the activity is in the foreground. 
     * Otherwise an IllegalStateException is thrown. 
     */ 
     setupForegroundDispatch(this, mNfcAdapter); 
    } 

    @Override 
    protected void onPause() { 
     /** 
     * Call this before onPause, otherwise an IllegalArgumentException is thrown. 
     */ 
     stopForegroundDispatch(this, mNfcAdapter); 

     super.onPause(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 


    public static void setupForegroundDispatch(Activity activity, NfcAdapter adapter) { 
     final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     final PendingIntent pendingIntent = PendingIntent.getActivity(
       activity.getApplicationContext(), 0, intent, 0); 

     IntentFilter[] filters = new IntentFilter[2]; 
     String[][] techList = new String[][]{}; 

     // same filter as manifest - action, category 
     filters[0] = new IntentFilter(); 
     filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED); 
     filters[1] = new IntentFilter(); 
     filters[1].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); 

     adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 
    } 

    public static void stopForegroundDispatch(Activity activity, NfcAdapter adapter) { 
     adapter.disableForegroundDispatch(activity); 
    } 
} 

マニフェストです

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.srjo.pocnfcadapter"> 

    <uses-permission android:name="android.permission.NFC" /> 

    <uses-feature 
     android:name="android.hardware.nfc" 
     android:required="true" /> 

    <application 
     android:name=".MyApplication" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 


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

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

      <meta-data 
       android:name="android.nfc.action.TECH_DISCOVERED" 
       android:resource="@xml/filter_nfc" /> 

     </activity> 

    </application> 

</manifest> 

答えて

0

Android DevelopeあなたのアプリがACTION_NDEF_DISCOVEREDのようなNFCインテントをフィルタリングする可能性があります。

+0

ただし、次のようにするにはどうすればよいですか?チップのIDを取得し、それが私のIDならアプリを開きますか? –

+0

私はまだこの文書でこれを読んでいないので、Googleはそのような機能を提供していないと思います。 –

0

まず、あなたがフォアグラウンドディスパッチ(フォアグラウンドアプリ)を使用する必要がありますマニフェスト

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.survivingwithandroid.nfc" > 

.... 
<intent-filter> 
<action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data android:mimeType="text/plain"/> 
</intent-filter> 
<manifest> 

中にアプリを登録します。最後に、NFCAdapterを使用してコンテンツ(onNewIntent)を読み取ります。 詳細を書いたときは、posts about NFCと書いてあります。

関連する問題