-1

私が取り組んでいるアプリは、ビームを受け取り、onResume から processIntent(intent) 関数を呼び出す必要があります。私が抱えている問題は、ビームを受信すると、アクティビティ内にとどまるのではなく、アプリの完全に新しいインスタンスを開くことです (私は既に enableForegroundDispatch を呼び出しています)。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null){
        Toast.makeText(this, "No NFC on this device", Toast.LENGTH_LONG).show();
    }
    // Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    // Declare intent filters to handle the intents that the developer wants to intercept.
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/com.sample.mime/string");
    } catch (MalformedMimeTypeException e) {
        Log.wtf("mimeexception", e);
        e.printStackTrace();
    }
    intentFiltersArray = new IntentFilter[] {ndef};

... ...
}

public void onResume() {
  super.onResume();
  // Enable the foreground dispatch when the Activity regains focus.
  NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);

  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
  }
}

public void onPause() {
  super.onPause();
  // Disable the foreground dispatch when the Activity loses focus.
  NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

void processIntent(Intent intent){
    // do stuff
}

これがマニフェストのアクティビティです

<activity
        android:name="com.example.app.opensthisonebutshouldnt"
        android:label="@string/app_name"  >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name="com.example.app.shouldopenthisone"
        android:label="@string/title_activity_create_invoice"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />\
            <category android:name="android.intent.category.DEFAULT" />\
            <data android:mimeType="application/com.example.nfctest/invoice" />
        </intent-filter>
</activity>

入力してくれてありがとう

4

1 に答える 1

1
  1. application/com.sample.mime/string(で使用ndef.addDataType(...);) は、有効な MIME タイプのようには見えません。このタイプのビームを送信していてよろしいですか? できたとしても、Android で処理できるかどうかはわかりません。あなたに近い有効な MIME タイプはapplication/com.sample.mime(/string部分なし) です。/invoiceの部分も同様ですapplication/com.example.nfctest/invoice

  2. フォアグラウンド ディスパッチ インテント フィルターが Beamed NDEF メッセージの最初のレコードと一致しない場合、アプリはマニフェストのインテント フィルターに従って NFC イベントを受け取ることがあります。NDEF_DISCOVEREDインテント フィルタは、NDEF メッセージの最初のレコードのみに一致することに注意してください。

  3. Beamed メッセージに AAR (Android Application Record) が含まれており、NFC 関連のインテント フィルターのいずれもBeamed NDEF メッセージの最初のレコードMAINと一致しない場合、マニフェストで宣言された、アクション用のインテント フィルターを持つカテゴリ付きの最初のアクティビティLAUNCHERが開始されます。 . これはあなたの場合に起こるようです。そのため、フォアグラウンド ディスパッチが受信 NDEF メッセージと実際に一致することを再確認してください。そうしないと、アプリ マニフェストの定義に従って、アクティビティが再起動される可能性があります。

  4. チェックgetIntent()インすると、アクティビティが再開されたときに、アクティビティにonResume()現在関連付けられているインテント (つまり、最初にアクティビティを開始したインテント、または を使用して登録されたインテント) のみがチェックされます。ただし、登録したフォアグラウンド ディスパッチからのイベントは、アクティビティのメソッドに到着します。したがって、そのメソッドをオーバーライドして、そこで NFC イベントを処理する必要があります。setIntent(...)onNewIntent()

    @Override
    public void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            processIntent(intent);
        }
    }
    
于 2014-07-12T08:42:22.357 に答える