Set the category "Programs" programmatically

I have an application with the following lines in AndroidManifest:

<category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> 

My question is: is it possible to set these parameters programmatically? I mean, some users will be able to enable or disable the above options.

What this means is to save the application as a launch.

So, is it possible to put a condition on this so that I can do it depending on the user?

EDITION: I edited this question to add a Rawr sentence.

Here's what I do before calling my core business:

  Intent myIntent = new Intent(v.getContext(), MainActivity.class); myIntent.addCategory(Intent.CATEGORY_HOME); myIntent.addCategory(Intent.CATEGORY_DEFAULT); myIntent.addCategory(Intent.CATEGORY_MONKEY); startActivity(myIntent); 

If I use these commands inside my manifest, they work. I can’t leave my application by clicking the home button. Adding categories manually as described above does not work.

Any suggestions?

Thank you in advance!

+8
android android-intent
source share
2 answers

When creating an intent you can use addCategory for specific categories for that intent. According to android documents , when resolving this intention, only actions that provide all the specified categories will be used. For example, if you did

 Intent i = new Intent(); i.addCategory(Intent.CATEGORY_HOME); i.addCategory(Intent.CATEGORY_DEFAULT); 

You can add a home and default category to your intentions along with any other custom categories you want. What you do with it is up to you. I can’t say what the purpose of the intention is, so I will leave it to that.

Regarding the behavior you presented in the commentary, I think this is due to the need for an intent filter. An intention category is not set if you still have not enabled a filter to respond to this category.

 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 

Setting an intent filter in your manifest will effectively listen to the categories you set programmatically. That's why you still need the appropriate category tags in your manifest to catch the intent.

+5
source share

I came across another way to do this - essentially, you cannot do what you ask, but you can disable / enable specific Activities your application.

That way, you can disable your home application by default, and then if the user wants to use it, they can enable it programmatically.

My information is derived from the following answer @Commonsware, which I duplicate here:


You cannot enable, disable, or create <intent-filter> programmatically.

However, in your case, you only have one <intent-filter> for each component. In this case, you can enable and disable the component programmatically using PackageManager and setComponentEnabledSetting() . In your case, enabling or disabling activity will have the same basic effect as enabling or disabling its <intent-filter> .


  • above answer originally @Commonsware to another thread
  • here is another helpful @mlc answer
+3
source share

All Articles