1

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>

3 Answers3

1

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()

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Thanks for this answer. I think it has the ingredient I need. –  Sep 13 '19 at 03:39
  • It seems Intent.EXTRA_EXCLUDE_COMPONENTS is for api 24, and I am using android 5. So I am stuck with the white list version, Intent.EXTRA_INITIAL_INTENTS. –  Sep 13 '19 at 17:01
  • Then there is the problem with intent chooser. I still want users to be able to set a default app for opening their files. –  Sep 13 '19 at 17:05
  • Look at https://stackoverflow.com/a/38351425/769265 to see how to do this. – David Wasser Sep 13 '19 at 17:09
0

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.

  • Thank you for your kind aid, I am humbled. However, once someone is in my app I know, since it is my app that creates and starts the intent in order to open the file. PROBLEM IS, my app intent filter in manifest, associates it to open any file. Useful when you download a file. Just click it from noti bar and my app opens up ready to rename it. BUT if you are already in my app, then this feature is redundant. Since it is opening the same app again to do something you could have done by clicking rename in app. –  Sep 12 '19 at 04:30
  • You are using PendingIntents for the notifications click, right? If so, check out the link I added in my edit to the answer. – Akriti Sondhi Sep 12 '19 at 04:36
0

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.

user1506104
  • 6,554
  • 4
  • 71
  • 89