NFC detection: either start activity or display dialog - android

I have a main-activity which shows some data in a list and a nfc-asynctask which reads some data from a card. I want to achieve the following behavior:
If the app is closed and a card is put near the mobile phone, the main-activity and simultaneously the nfc-asynctask should be started. The results of the asynctask should be presented in a dialog.
If the app is opened and a card is put near, the nfc-asynctask should be restarted and only a dialog with the results should be opened.
My current approach always starts the main-activity. This means that sometimes, there are multiple instances of my main-activity and when the user hits the back-button, instead of switching to the home-menu, another activity instance is put on.
Manifest
<activity
...
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="#xml/filter_nfc"/>
</activity>

Have a look at Android's foreground dispatch facility. If you register your app for foreground dispatch, your activity receives an onNewIntent() event instead of getting started a second time.
Also, I suggest putting the TECH_DISCOVERED intent in a separate intent filter:
<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.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/filter_nfc" />

Related

Receive data intent opens second app when app is opened

<activity
android:name=".activitys.SplashScreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
When app is sterted, trying to share the text and app is launching but if open pervious app by android navigation button i see 2 my app exemplars. How to check if app is launched and reopen not open second at one time
Simple answer: you need to add
android:launchMode="singleInstance"
within your activity bracket (below android:name=".activitys.SplashScreen", for example)
Better answer: Go through this article to understand the different types of launch modes: https://medium.com/#iammert/android-launchmode-visualized-8843fc833dbe
Best answer: Read through the doc on tasks: https://developer.android.com/guide/components/activities/tasks-and-back-stack

What does it mean when two or more activities, each having intent-filter with action=android.intent.action.ACTION_MAIN?

Doc says https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN is an entry point.
Example code:
<activity android:name="org.A.A"
android:theme="#style/NoTitle"
android:screenOrientation="behind"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.C"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.B"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
1) So using android.intent.action.ACTION_MAIN acts as an entry point to the parent component (parent component I mean the activity or receiver or service)?
2) If yes, Entry point from where, as there is no CATEGORY mentioned.
Android apps are composed of different components. e.g. Activity, Service, BroadcastReceiver, and ContentProvider and each component can act as an entry point of the app.
Let's take activity as an example, you have defined an activity in your application with following action
<intent-filter>
<action android:name="com.yourapp.SOME_ACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
and I start an activity in my app with following intent.
Intent intent = new Intent("com.yourapp.SOME_ACTION"); // same action
startActivity(intent);
Now what will happen? System will search for activities with com.yourapp.SOME_ACTION action and if it finds one (in current scenario, it will be activity you have created with com.yourapp.SOME_ACTION in your app), it will start your app (if it is not already started) and will open the activity in your app.
See, now I can enter into your app by using Activity with com.yourapp.SOME_ACTION. Same thing happens in case of other components.
Activity with MAIN action will be entry point of application.If you have one its well and good if more than one then you can have multiple activities through which you can enter into application.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
But if you have provided CATEGORY for activity it will create launcher for that entry point.
<category android:name="android.intent.category.LAUNCHER" />
Suppose two activities is having both MAIN action and CATEGORY as launcher two app icon will be created one will have one activity as entry point second will have another one as entry point.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android - Reacting to assist request

I am programming a tiny app which opens the camera as it is opened via assist-request.
In the onCreate method, it simply opens the camera via intent.
But if the app is opened "the regular way" I'd like to display a little instruction instead of the camera.
Is there an easy solution for checking if the app was started by an assist-request?
extract from my Manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If you want to determine which sort of Intent was used to start MainActivity, use getIntent().getAction() and compare it to the possible values (e.g., Intent.ACTION_MAIN).

Multiple Android deep link, app goes in onResume()

I have an html page with two links as follow:
Test 1
Test 2
I open it with android Internet app. In my "Hello word" manifest application i declare:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter android:label="testapp">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myApp" />
</intent-filter>
</activity>
I can open my application with both links.The first time i click on both links my app starts and passes through onCreate (my app was not in background before click). The second time (app now is in recent tasks), the first link clicked before in chronologically order pass through onResume only but the second pass on onCreate. This will be the behaviour if i try open links n times until i kill application from recent. Any ideas?Thanks!
An Android activity, by default can be instantiated multiple times. You should use android:launchMode="singleTask" to override this behavior. There are some other flags that might interest you if you - here is the link to documentation.

android) can i set a default activity that runs right after installing

in my app, there is two activity and i want to make activity1 to the starting activity after installation.
But now the RUN button (is showed right after packgae installing) is disabled.
below is the manifest file. thanks.
<activity ...1>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity ...2>
<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.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</activity>
android) can i set a default activity that runs right after installing
No activity "runs right after installing". The user has to launch it from the launcher.
below is the manifest file
No, it is not. That is not even valid XML.
Also, note that your third <intent-filter> is invalid. Not only are you missing any category (you need at least DEFAULT for activities), but ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED are not activity actions.
I am going to take a guess that you really mean to ask: "I have two activities, both described as ACTION_MAIN/CATEGORY_LAUNCHER, and now the Run button does not work -- what can I do?" The answer would be "either remove the ACTION_MAIN/CATEGORY_LAUNCHER <intent-filter> from one of them, or mark one of the two as disabled (android:enabled="false") and enable it later on using PackageManager."
I think the problem is the second:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You shouldn't have 2 activities flagged as the MAIN and LAUNCHER activity.
Try removing it within activity2.
Check out: http://developer.android.com/reference/android/content/Intent.html the intentfilter s are discussed.

Categories

Resources