Prevent Android Activity from launching it self - android

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.

Related

Why is my app not showing in the grid of icons (not the launcher) after installation

I recently started testing my react-native android application and noticed that it doesn't create an app icon on installation. When I debug - it seems to install and run fine, but when I look for it in the app icon to launch - it does not show up. Looking at the list of installed apps, it's there. How can I debug this? Do I need to change the launch mode from singleTop to something else? I tried changing it to singleInstance and it still had the same issue. If I remove some of the intent filters it seems to work - so maybe I messed up the branch.io installation instructions? Do I need to create a seperate intent filter xml node for the other intents? https://dev.branch.io/getting-started/sdk-integration-guide/guide/react/#android-configure-manifest
AndroidManifest.xml
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<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="www.foo.com"
android:pathPrefix="/event" />
<!-- note that the leading "/" is required for pathPrefix-->
<data android:scheme="fooapp" android:host="open" />
</intent-filter>
<intent-filter>
<action android:name="fcm.ACTION.EVENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Update
Splitting up the intent-filters seems to have fixed it - but a bit unclear if that is the correct thing to do. In the branch.io instructions - did they mean for me to create a new intent-filter and not add to the existing main one?
<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="www.foo.com"
android:pathPrefix="/event" />
<!-- note that the leading "/" is required for pathPrefix-->
<data android:scheme="fooapp" android:host="open" />
</intent-filter>
The launcher is typically specific software developed by the individual hardware manufacturers. On all of the devices i have, an app does not get installed on the homescreen when it is not installed from the play store, but it does show up in the list of apps. However, that behaviour will be specific to the launcher you are using i think.
You should not need to go to settings->apps though just to see it. Is that the problem you are having?
Perhaps a screenshot as well as a description of your test device (s) would make the question clearer.
Update....
Typically the startup activity has one intent filter with only MAIN and LAUNCHER. I would just try to remove the other things from that intent filter and see if that resolves the problem. Then one by one, add the rest of the things you have listed until you identify the problem child.
Looking at this again, I am not sure that you can define 2 actions in a single event filter. So it seems you need to remove VIEW.

App icon not visible when code is run

I have created an android app using android studio. When i run my code on the IDE, its runs as expected on the connected phone. The problem is, it does not leave behind an icon on my phone to use it next time. I will again have to run from android studio to use the app. This was fine initially, i don't realize what made the icon vanish. The app is also present in the settings.
When i go to
Settings->apps
i can very well find my app over there. It is not disabled. I tried uninstalling the app and trying again. It still doesn't give me the icon.
I cleared defaults, forced closed and tried everything possible. Nothing is helping me.
Please help.
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TwitterLogin"
android:label="#string/title_activity_twitter_login" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<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.chillumapp.com"
android:pathPrefix="/chillum"
android:scheme="http" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".NextActivity"
android:label="#string/title_activity_next" >
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="b1fe9f0c7d80c3c24879d634b199f2d0e474b4ba" />
<activity
android:name=".WebViewActivity"
android:label="#string/title_activity_web_view" >
</activity>
</application>
</manifest>
You should specify a different for every intent that can launch your app. So one for the BROWSER category with your url data, and one for the LAUNCHER category.
From the App Manifest Guide.
Components advertise their capabilities — the kinds of intents they can respond to — through intent filters. Since the Android system must learn which intents a component can handle before it launches the component, intent filters are specified in the manifest as elements. A component may have any number of filters, each one describing a different capability.
Your code would become something like:
<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:host="www.chillumapp.com"
android:pathPrefix="/chillum"
android:scheme="http" />
</intent-filter>

Open activity after intent 'click on phone number'

I found several answers on this issue, but it's not working for me. If you click a phone number in an email or on a website, a default dialer popup comes up to select the dialer/skype etc.
I'm trying to get my app in that list - so I don't want to handle the actual call, but open the activity and show the number the user clicked on.
I've tried this:
<receiver android:name=".MyOutgoingCallHandler">
<intent-filter>
<action android:name="android.intent.action.ACTION_DIAL" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
But it's not showing in the list. What intent should I filter for?
If you want to add an Activity to the list of Activitys that will be shown to the user as possible phone dialers, you need to have an Activity and the <activity> definition must contain this intent-filter> in the manifest:
<activity ...>
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What you've got is a BroadcastReceiver that will receive the outgoing call Intent. That's something different.
The official Android Developers blog has covered this process. You can read all about it there.
Your intent filter looks like it has the correct action, so it may be that you have not requested the correct Android permission.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Here is how the Android Developers blog suggest you declare the broadcast receiver.
<manifest>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application>
...
<receiver android:name=MyOutgoingCallHandler">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
...
</application>
</manifest>

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

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"

Listen Broadcast Before application uninstall

I have installed Avg Antivirus for testing purpose.
After Testing when I have tried to uninstall that Antivirus,Antivirus has prompted me that 'are you sure you want to remove this application?'.
That prompt screen is generated by antivirus application. After that screen I got System prompt with OK and CANCEL Button. so, That prompt was put by antivirus application.
Now my question is...
How can I put prompt screen for user in my application as same as explain above?
I know about "android.intent.action.PACKAGE_REMOVED" but it received after application has been fully uninstall.
Any help is appreciated.
Thank You.
I've added the following intent-filter and now I receive the dialog to chose activity. I think you should play with this filter to display your activity (especially with data part of the filter).
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".UninstallIntentActivity"
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" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
</application>

Categories

Resources