2

私は od NFC タグを利用するアプリケーションを書いています。NFC タグをアプリケーションへのエントリ ポイントにしたい - 電話までのタグを含むカードを閉じると、アクティビティが開始されます。次に、アクティビティが実行されているときに、NFC タグを使用できるすべてのインテント フィルターを無効にしたいので、別のクローズアップでは何もしません (アクティビティを最初からやり直したくありません)。アクティビティ エイリアスを使用して、インテント フィルターを「無効にする」方法を知っています。

    <activity-alias android:enabled="true"
            android:name=".alias.NFCEntryPoint"
            android:targetActivity="pl.mitrue.safedb.NFCEntryPoint"
            android:exported="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/alias.pl.mitrue.safedb.NFCEntryPoint" >
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

次に、プログラムでエイリアスを無効にできるため、インテント フィルターも無効になります。

getPackageManager().setComponentEnabledSetting(new ComponentName("pl.mitrue.safedb", "pl.mitrue.safedb.alias.NFCEntryPoint"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

ここで問題があります。私のデバイスには、この種のフィルタを使用する他のアプリケーションがインストールされています。ここで、アプリケーションの実行中に (アクティビティ エイリアスを無効にして) インテント フィルターを無効にすると、使用するアプリケーションの選択を求めるダイアログが表示されます。これを回避する方法はありますか?最も単純なシナリオでは、アプリケーションが開始されたら、NFC タグを閉じるときに何のアクションも実行したくありません。外部インテントを受信できる唯一のアプリケーションにする方法はありますか?

記録のために: NFC を完全にオフにしたくはありません (それが可能かどうかさえわかりません)。

私の主張を明確にしたことを願っています。私はここで新しいので、理解してください。

ありがとう!

4

1 に答える 1

1

私は同様の問題を抱えていました.1)セルの開始画面(すべてのアプリが閉じられている)にいた場合、mifareクラシックカードを渡すと、アプリが自動的に開きます。ミファーレ クラシックを読みます。

2) アプリが開いていてカードを渡す場合は、アプリを閉じずに、mifare classic も読み取るアプリがたくさんある場合は、どのアプリを使用しているかを尋ねます。解決策:フォアグラウンド ディスパッチを使用する

私の主な活動では:

private IntentFilter[] intentFiltersArray;
private String[][] techListsArray;
private PendingIntent pendingIntent;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("text/plain");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        intentFiltersArray = new IntentFilter[] {ndef, };
        techListsArray = new String[][] { new String[] { android.nfc.tech.MifareClassic.class.getName() } };
    }

@Override
    protected void onResume() {
    super.onResume();
    adaptadorNFC = NfcAdapter.getDefaultAdapter(this);
    System.out.println("Se setearan parametros");
    final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    adaptadorNFC.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

    leerTarjeta(this.getIntent());
}

@Override
protected void onPause() {
    System.out.println("Se dessetearan parametros");
    adaptadorNFC.disableForegroundDispatch(this);
    super.onPause();
}

この質問が1年前に尋ねられたので、これが他の人に役立つことを願っています:P

于 2015-01-15T11:16:59.203 に答える