Android Studio disable NFC - android

How can I make NFC tag scanning happen only in selected activities? Currently, scanning takes place in every activity and every time the phone is applied to the tag.
I tried to solve the problem from this link but the application does not turn on at all.
Android app enable NFC only for one Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.nfc_scanner">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.NFC_Scanner"
tools:targetApi="31">
<activity android:name=".MainActivity"
android:exported="true"
tools:ignore="WrongManifestParent">
<intent-filter>
<!-- MAIN represents that it is the Main Activity-->
<action android:name="android.intent.action.MAIN" />
<!-- Launcher Denotes that it will be the first launching activity-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RentActivity"
tools:ignore="WrongManifestParent">
</activity>
<activity android:name=".AddingCostumesActivity" android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/tech_list" />
</activity>
</application>
<uses-permission android:name="android.permission.NFC"
tools:ignore="ManifestOrder" />
</manifest>

So only the user can actually disable NFC by going to the settings BUT you emulate that by handling NFC in every Activity and just doing nothing when a Tag is detected in Activities that you want it to look like NFC is disabled.
To do this I would use the better enableReaderMode API Example as you can also turn off the NFC sound with that API (plus it is more reliable especially for writing and also automatically handles NFC in a non UI thread as per documentation and does not have pause and resume your app to send it NFC data).
This is very similar to the answer you linked to but also silences the detection sound.
So remove the manifest filter lines
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/tech_list" />
Implement enableReaderMode in all Activity classes and if you want to make it look like NFC is disabled then use an onTagDiscovered method of:-
public void onTagDiscovered(Tag tag) {
}

Related

can't open android app from menu/ problems with permisions

I have a problem with launching this app through menu. There is no problem when I start it using a different application that has permission. This is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.michael.dangerouslab" >
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"
android:protectionLevel="dangerous"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:permission="course.labs.permissions.DANGEROUS_ACTIVITY_PERM">
<activity android:name=".DangerDanger" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="course.labs.permissions.DANGEROUS_ACTIVITY"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
Delete both occurrences of android:permission="course.labs.permissions.DANGEROUS_ACTIVITY_PERM" (one in <application>, one in <activity>).
Those say "no third-party app, including the home screen, can do anything with my app, unless they have <uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/> in their manifest". There are roughly zero apps that have such an element, including roughly zero home screen implementations. Hence, you will not be able to launch your app.

Android mobile app always get restarted once it is connected or disconnected to bluetooth device

We have Android native multiple tab barcode application which contains barcode field for Bluetooth device to enter the barcode. When lunch this mobile, it first have one popup to let user to select the tab. The problem is that every time when the bluetooth device is connected or disconnected to the mobile, the application always get re-lunched with this popup. How can we stop mobile to re-start when the bluetooth device is connected or disconnected?
I'm attaching the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mentor.med.mobile"
android:versionCode="1"
android:versionName="1.0.1" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="#drawable/med"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppTheme" >
<activity
android:name="com.pdx.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustPan"
android:launchMode="singleTop"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="pdx.com" android:path="/view" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The application probably registers a broadcast receiver, which listens for bluetooth devices being connected. That receiver then launches the Activity with a popup. You have a few options:
One solution could be to remove the entry in the manifest file which registers the intent filter to a receiver (it could be an activity). But obviously then the app will never launch automatically when Bluetooth devices are connected.
Alternatively, you probably want to change the logic of the receiver so it checks if the activity is already open before re-opening it. One way would be to do this in the Activity itself: depending on how you want the application to behave, you may need to change launchMode="singleTop", and use override methods such as isFinishing(), onNewIntent() to determine if the application is restarting and why.

Android manifest for USBAccesory

I am making an app that communicates with a piece of usb hardware made by my company (this is the only app allowed to talk to the usb accessory, it's not a public api). I am having difficulties setting up the proper launch modes in the manifest.
There are three components to the app: the main activity, a login activity, and the USBService.
I'm assuming the intent for the main goes to the login activity, and the intent for the usb goes to the USBService, but I am not sure if I do this, will this start the service if the app is not running? More over, if it does, how do I fetch an already existing service?
What type of structure should I be looking at for the manifest file? (specifically, intent-filters, and appropriate launch modes... I've read a few documents about the launch modes but I am still not sure I quite understand... There should only ever be at most one instance of each activity/service, and they need to communicate together.
edit: it is not necessary for communications to start before the app is open, nor is it necessary to launch the app automatically when the usb is connected.
edit: my manifest as it stands, looks like:
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="mainpackage.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="#xml/accessory_filter" />
</activity>
<activity
android:name="mainpackage.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<service android:name="updater.USBService"
android:exported="false" >
<!--
-->
</service>
</application>
in your manifest add
<manifest ...>
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
In this case, the following resource file should be saved in res/xml/device_filter.xml and specifies that any USB device with the specified attributes should be filtered:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" />
</resources>
Hope this help.
Your manifest looks good,
I think you make a good choice for putting the intent-filter "android.hardware.usb.action.USB_ACCESSORY_ATTACHED" in the mainActivity and start the application in this activity,
and again I think it's a good choice to start your mainActivity in SingleTop launch mode,
because if an instance of the mainActivity already exists at the top of the current task, the system going to launch this activity, no new instance of this activity will be created.
For a best understanding of the different launch mode available in android,
I think this link may help you :
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
To make a long story short I think you'll be all set with this manifest as is.
To Use Android Devices min SDK version should be set to 12 and need to declare following line in AndroidManifest.xml file
<>
<uses-sdk android:minSdkVersion="<version>" />
...
<application>
<uses-library android:name="com.android.future.usb.accessory" />
<activity ...>
...
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="#xml/accessory_filter" />
</activity>
</application>

Android first app, all activities visible in menu

I just created my first Android app, all works fine, but all of the activities are shown on my Android menu. What did I do wrong?
Screenshot: imgur.com/9CmXU
I want to have one icon, directing to MainActivity, not all activities
<!-- language-all: lang-html -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myApps.birthdaymeter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/my_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BirthdayMeter"
android:label="#string/title_activity_birthday_meter" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AboutWindow"
android:label="#string/title_activity_about_window" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ShowResult"
android:label="#string/title_activity_show_result" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is my manifest, changing logo didn't help
The <intent-filter> tag is used to describe the type of Intents that each Activity will respond to. The "android.intent.action.MAIN" action says that this Activity is an entrance point for your Application (think the main method that a java program requires). The "android.intent.category.LAUNCHER" category tells the OS to display the Activity in your list of Applications. Adding the DEFAULT category is ok, but effectively the same as omitting the <intent-filter> tag completely (which I find to make for much cleaner and easier to read code). You should only be using the <intent-filter> tag with these two actions and categories on the Activity that starts when a user opens your app. If wanted an Activity within your application to be able to respond to some special intents, you would use the tag to define which it responds to.
Here's a link to the google dev pages to help you learn more about Intent Filters.
http://developer.android.com/guide/components/intents-filters.html
Here's the documentation for the <intent-filter> tag. It's not the easiest to understand though.
http://developer.android.com/guide/topics/manifest/intent-filter-element.html
And here's the docs for the Manifest file and the Intent class. Both of these are good for reference if you're not sure about which tags to use in your Manifest. Good luck!
http://developer.android.com/guide/topics/manifest/manifest-intro.html
http://developer.android.com/reference/android/content/Intent.html
You phrased your question extremely generally, so I am not sure what to answer. It seems like you haven't called nameOfActivity.this.finish() when you launched another activity
If you post your manifest file I can help a little more, but it looks to me like you have not declared your activities correctly.
It should be setup somewhat like this:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="Activity1"></activity>
<activity android:name="Activity2"></activity>
<activity android:name="Activity3"></activity>
</application>
Essentially, create an application declaration, and then include each activity within it.

How can i read NFC tag in android?

Hi i am developing an android application using NFC feature. Here i tried to read NFC Mifare nfc tag. I used NFCDemo which is available with android api.But i did not get success to read data through my application. In that demo that is always reading fake tags and giving fake result only.
I have a confusion about the manifest file intent filters. In my demo application is like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc"
>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<activity android:name=".simulator.FakeTagsActivity"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="TagViewer"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
And the application is always starting with FakeTagsActivity activity any showing list of fake tags.When we click on any one of the fake tags it will redirect to TagViewer activity with fake data not the real tag data.
I have confusion too in TagViewer activity also that is resolveIntent(Intent intent) is handling always
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {}
but i am not understanding whats the wrong with this.
Please advice me am i need to modify the api demo to read real tag data. Or advice me is there any other way to read the nfc tag.
I already tried This Sample
but not help full for me.
Please help me. Thanks in advance.
Finally I figured out solution for my question. There we need to update the intent filter of TagViewer activity like
<activity android:name="TagViewer"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Then I modified my if condition of TagViewer class like
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {}
It will working fine now. The main problem is priorites of TAG detection those are
priority 1 : NDEF_DISCOVERED
priority 2 : TECH_DISCOVERED
priority 3 : TAG_DISCOVERED
I gave the priority 1 for my application intent filter then the android system always starting my activity when the tag detected.
Check with this for NFC available or not a link and try this for reading tag details a link..... Add readind tag details code in seperate active..(eg: youractivity) and in manifest give as
<activity android:name=".youractivity"
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="text/plain"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Categories

Resources