How can I access to my application which is already running? - android

I run my application "A" normally with the launch icon in the "Applications" menu.
I press the home button, so my application "A" is still running on my phone.
Now I browse my files presents in my phone, and I use the share action to share this file with my application "A".
The file is sharing perfectly, but instead of using the instance of my application already opened, it opens a new instance of my application "A".
If I quit this new instance, the first instance is still running and it's a problem concerning the security goal of my application.
I try to use the FLAG_ACTIVITY_CLEAR_TOP to use the activity in the first instance, but it doesn't work because it isn't the same application which is launched by the OS.
Is there a way to do this ? And if yes can you give me some hints or some leads to follow ?
My manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity android:name=".SplashScreenActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExplorerActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.Light.NoTitleBar" >
</activity>
<activity
android:name=".ChooseDialogActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
</application>
Here is my manifest. Usual process : SplashScreenActivity -> ExplorerActivity
Share process : ChooseDialogActivity -> ExplorerActivity
What I want, it's that the second ExplorerActivity has to be the same that the first ExplorerActivity if this activity already exists.

Please don't use launchMode="singleInstance". This is not what you want. singleInstance is only for HOME-screens and similar apps.
Try instead to use Intent.FLAG_ACTIVITY_NEW_TASK when sharing. This will separate your application from the files-browsing application and may get the behaviour you want. You may also need to add FLAG_ACTIVIY_CLEAR_TOP as well, depending on how you've programmed your app.
EDIT
When you launch ExplorerActivity from ChooserActivity, do this (or something similar):
Intent intent = new Intent(this, ChooserActivity.class);
intent.addFlags(Intent.ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If there is already a task running that contains the ChooserActivity, this should bring that task to the foreground instead of creating a new instace of ChooserActivity.

use the launchmode this will keep one instance from your main activity so make this change for the home activity .
<activity ..
...
..
android:launchMode="singleInstance" />

try android:launchMode="singleInstance"

Related

React Native deeplinking and android:launchMode problem

I have been trying deeplinking in a react-native application and trying to directly open a screen inside a navigator. I use react-native-deep-linking and react-navigation as libraries. Also nested navigators are used. Deeplinking works correctly except I have some problems with the android:launchMode property.
This is the results I get for each of the android:launchMode options.
android:launchMode="standard" - App opens using the deeplink but opens up an entirely new application.
android:launchMode="singleTask" - App opens using the deeplink. If I open the app using another link. App comes to the foreground but it directs to the previous link.
android:launchMode="singleTop" - App opens using the deeplink but opens up an entirely new application.
android:launchMode="singleInstance" - App opens using the deeplink but opens up an entirely new application.
If I remove the android:launchMode property, again the same thing happens as the "standard" mode because it is the default.
What option can I use to resolve this problem? Is there any #override that I can do inside the main activity?
Below is my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".MainApplication"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<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"/>
<category android:name="android.intent.category.VIEW"/>
<data android:scheme="https" android:host="*.myapplive.com" android:pathPrefix="/"/>
<data android:scheme="https" android:host="*.myapp.com" android:pathPrefix="/"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
</application>
Not sure if still relevant but may help someone in future.
The reason is that when activity is in any mode other than standard, re-opening the app from the background will not replace the original intent used to launch the app from the killed state. Instead, those new intents will be directed to the onNewIntent(Intent intent) method. There, you can call setIntent(Intent intent) in order to replace activity's intent with the new one.
You can override this method in your MainActivity. (Note that when overriding this method in React I had to make it public and not protected as I would usually do in Android. Not sure why yet)
Read more here - https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

Prevent Android Activity from launching it self

MY PLIGHT EXPLAINED
I cannot figure out or find out to stop my Activity from launching itself. This is necessary as I am making a dir manager that has intent filter to open any file, in attempt to easily locate and rename it. For instance clicking a file from notification to be opened by my app for renaming.
MY PLIGHT SUMMARIESED
But it makes no sense to run this intent if you are already in my app. Thus I want to programatically stop my app from being a choice in this case.
MY TRIES FOR SOLUTION
I have tried many stuff. Google, android reference on ActivityInfo. I even tried creating my own activity chooser dialog. Which doesnt work 100% accurately. If it did I could simply omit my activity from the list.
I humbly seek assistance.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="jav.android.dir_mgr">
<uses-sdk android:minSdkVersion="12" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:icon="#mipmap/app_icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".DirectoryExplorer"
android:screenOrientation="portrait" >
<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" />
<data android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
<activity
android:name=".SelectionWindow"
android:screenOrientation="portrait"
android:label="Selection Window"
android:allowEmbedded="true">
<intent-filter>
. <action android:name="jav.android.dir_mgr.SELECT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You can use EXTRA_EXCLUDE_COMPONENTS to exclude your own application from the list shown to the user. See the documentation which states:
You can exclude specific targets by providing
Intent.EXTRA_EXCLUDE_COMPONENTS. This is to be used only to remove
targets you have control over. A common use case is to hide your app’s
share targets when your users share from within your app as their
intent is likely to share outside your app.
Add Intent.EXTRA_EXCLUDE_COMPONENTS to your intent after calling
Intent.createChooser()
So, from what I understand, you need a way to check if your app is in focus or not. If that's so, try using ActivityManager.getRunningAppProcesses() which will return a list of application processes that are running on the device.
From there you can iterate through processes that are running on the device and check if your app is listed there and that it's of RunningAppProcessInfo.IMPORTANCE_FOREGROUND.
EDIT: I assume you are using pending intents, then you won't be able to check if your app is already running in the above way. Try using a "launch intent" for your app as explained in this answer.
I hope I understood your plight correctly.
One way to solve your problem is to programmatically unregister your activity from listening to a certain intent-filter value. So let's say you have an activity declared as:
<activity android:name=".FileHandlerActivity">
<intent-filter>
<action android:name="you-file-rename-action-name-here"/>
</intent-filter>
</activity>
Do this in your app:
PackageManager pm = getApplicationContext().getPackageManager();
ComponentName component = new ComponentName(getPackageName(),
getPackageName()+".FileHandlerActivity");
pm.setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Please let me know if this works.

Android Deep linking and singleInstance/singleTask

Possible Duplicate Deep linking and multiple app instances. I have implemented Deep Linking in my app. I have Splash activity that is launcher and MainActivity that handles the Intent as defined in manifest:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:largeHeap="true"
android:theme="#style/AppTheme">
<activity
android:name=".ActivitySplash"
android:configChanges="orientation|screenSize"
android:label="#string/app_name">
<intent-filter>
<!-- Launcher activity -->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityMain"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|screenSize"
android:exported="true"
android:label="#string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<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:host="www.mywebsite.com"
android:pathPrefix="/something"
android:scheme="http" />
</intent-filter>
</activity>
<activity
android:name=".ActivitySignIn"
android:configChanges="screenSize|orientation" />
<activity android:name=".ActivitySignUp" />
</application>
I have set launch mode singleTask to handle onNewIntent(). Now what i want to achieve is that if user opens activity from DeepLinking and there is already some task going on in MainActivity I prompt user a dialog either he want to cancel current task and start new task (which is from deep linking). The issue is If i open another activity from MainActivity and user comes from DeepLinking Intent. Then it would kill the second activity and directly open MainActivity. What i want to achieve is that if app/activity is not running then Intent from DeepLink open as is. And if activity/app is already running then i prompt user to either close current task and perform DeepLink task/intent.
This doesn't really work the way you think it does. You are trying to use launchMode="singleTask", but since you haven't also set "taskAffinity", Android pretty much ignores your launchMode.
You should not need to use either of the special launch modes "singleTask" or "singleInstance" to get what you want.
Try using singleTop launch mode and see if this solves your problem. If ActivityMain is already open and you launch ActivityMain again using your deep-link, onNewIntent() should be called in ActivityMain.
You can also look at my answer to this question which describes a way to determine what Activity to show based on using a static variable to decide whether another Activity is in the stack or not.

How to hide android app from launcher

I would like to hide my android app from the launcher, but be able to call it from within another app. I am lost on what to remove from the android manifest.
Already tried removing...
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
...but then it doesn't open when called from another app.
Here is what how I am calling this hidden
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("org.xbmc.xbmc");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
Here is the top of the manifest
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher" android:hasCode="true" android:debuggable="true">
<activity android:theme="#*android:style/Theme.NoTitleBar.Fullscreen" android:name=".Splash" android:finishOnTaskLaunch="true" android:launchMode="singleInstance" android:screenOrientation="sensorLandscape" android:configChanges="touchscreen|keyboard|keyboardHidden|navigation|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
You need to remove the following line from your AndroidManifest.xml:
<category android:name="android.intent.category.LAUNCHER"/>
This will remove the application from the default launcher. However, you also need to add the following line such that your BroadcastReceiver is not completely ignored:
<category android:name="android.intent.category.DEFAULT"/>
You should NOT remove the line below - it is used to specify which Activity should launch first when your app is opened:
<action android:name="android.intent.action.MAIN"/>
EDIT
In order to launch the application discussed above from another application, you cannot use the calls shown in your question. You are trying to open the application by creating an Intent with the CATEGORY_LAUNCHER tag (i.addCategory(Intent.CATEGORY_LAUNCHER)) when you have explicitly removed the following line from your AndroidManifest.xml file:
<category android:name="android.intent.category.LAUNCHER" />
The absence of the above line means that the application you are trying to call will ignore the launch Intent. In order to launch your application you will need to act upon another Intent. Here is an example that shows how to open an application, which doesn't contain a launch intent filter, by responding to a SMS Intent: How to launch an Android app without "android.intent.category.LAUNCHER"
Which intent you choose to use is up to you - just make sure you add it to your AndroidManifest.xml file.
Try this code:
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
and check this link.
hope this helps.

What does it mean "No Launcher activity found!"

I am writing a simple program of Android, and getting these no errors, I don't know what they are. My program is right, but showing not output.
I think it is because of these two lines:
[2005-01-06 19:56:38 - my_Android] No Launcher activity found!
[2005-01-06 19:56:38 - my_Android] The launch will only sync the application package on the device!
Here's an example from AndroidManifest.xml. You need to specify the MAIN and LAUNCHER in the intent filter for the activity you want to start on launch
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name="ExampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Multiple action tags in a single intent-filter tag will also cause the same error.
Like Gusdor said above, "Multiple action tags in a single intent-filter tag will also cause the same error." (Give him the credit! I could just kiss Gusdor for this!)
I didn't find any docs for this fact!
I had added a new (USB) action and being clever, I lumped it in the same intent-filter. And it broke the launch.
Like Gusdor said, one intent filter, one action!
Apparently each action should go in its own intent filter.
It should look like this...
<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_DEVICE_ATTACHED" />
</intent-filter>
When I did this, WAZOO! it worked!
Do you have an activity set up the be the launched activity when the application starts?
This is done in your Manifest.xml file, something like:
<activity android:name=".Main" android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Check your manifest.xml. Make sure you have the category LAUNCHER there.
<activity android:name=".myActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It means you didn't specify an Activity for Android to launch as the default when the app opens from the launcher. You have to add an Intent Filter in the Manifest for the Activity you would like to act as the default when the app is being launched.
Read http://developer.android.com/guide/topics/intents/intents-filters.html#ccases for more details.
I fixed the problem by adding activity block in the application tag. I created the project using wizard, I don't know why my AdroidManifest.xml file was not containing application block? I added the application block:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ToDoListActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And I get the desired output on the emulator.
As has been pointed out, this error is likely caused by a missing or incorrect intent-filter.
I would just like to add that this error also shows up if you set android:exported="false" on your launcher activity (in the manifest).
I had this same problem and it turns out I had a '\' instead of a '/' in the xml tag. It still gave the same error but just due to a syntax problem.
If you are using the standard eclipse IDE provided by google for Android development, you can check the "Launcher Activity" check-box while creating a new Activity. Please find below:
In Eclipse when can do this:
But it is preferable make the corresponding changes inside the Android manifest file.
You missed in specifying the intent filter elements in your manifest file.Manifest file is:
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name="Your Activity Name"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Add and check this correctly. Hope this will help..
Manifest is case sensitive, so please compare this lines for any case mismatch especially the word MAIN in:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
You can add launcher to activity in eclipse manifest visual editor:
MAIN will decide the first activity that will used when the application will start.
Launcher will add application in the application dashboard.
If you have them already and you are still getting the error message but maybe its because you might be using more than more category or action in an intent-filter. In an intent filter there can only be one such tag. To add another category, put it in another intent filter, like the following
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--
TODO - Add necessary intent filter information so that this
Activity will accept Intents with the
action "android.intent.action.VIEW" and with an "http"
schemed URL
-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
just add this to your aplication tag in AndroidManifest.xml file
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and also edit the uses-sdk tag from android:targetSdkVersion="16" to 17
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
You have not included Launcher intent filter in activity you want to appear first, so it does not know which activity to start when application launches, for this tell the system by including launcher filter intent in manifest.xml

Categories

Resources