You're using the NFC and Tech Discovered intent-filters
. You could remove them and register intent listeners programmatically in your app.
<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.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
About registering the intent: Here's a very good answer.
In your onCreate
method you can register a receiver like this:
private BroadcastReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState){
// your oncreate code should be
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
filter.addAction("SOME_OTHER_ACTION");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
};
registerReceiver(receiver, filter);
}
Remember to run this in the onDestroy
method:
@Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}
Another interesting quote from the answer below that one:
One important point that people forget to mention is the life time of
the Broadcast Receiver
. The difference of programmatically
registering it from registering in AndroidManifest.xml is that.
In the manifest file, it doesn't depend on application life time.
While when programmatically registering it it does depend on the
application life time. This means that if you register in
AndroidManifest.xml, you can catch the broadcasted intents even when your application is not running.
Edit: The mentioned note is no longer true as of Android 3.1, the Android system excludes all receiver from receiving intents by default
if the corresponding application has never been started by the user or
if the user explicitly stopped the application via the Android menu
(in Manage → Application).
https://developer.android.com/about/versions/android-3.1.html
This is an additional security feature as the user can be sure that
only the applications he started will receive broadcast intents.
So it can be understood as receivers programmatically registered in
Application's onCreate()
would have same effect with ones declared
in AndroidManifest.xml from Android 3.1 above.
If you unregister your receivers when your activity is killed, your application won't receive these broadcasts anymore.