Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Intents category programmatically

I have an application with the following lines in the 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 options programmatically? I mean, some users will be able to enable or disable the options above.

What this does is to keep my app as a launcher.

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

EDITION: I edited this question to add the suggestion of Rawr.

Here is what I am doing before calling my main activity:

        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. One cannot leave my app by pressing home button. Adding categories manually like above doesn't work.

Any suggestions?

Thank you in advance!

like image 266
Carlos Pereira Avatar asked May 08 '13 17:05

Carlos Pereira


2 Answers

When you create an intent you can use addCategory to specificy categories for that intent. According to the android docs, when resolving this intent only activities that provide all of the categories specified will be used. So for example if you did

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

you could add the home and default category to your intent along with any other custom categories you wish. What you do with it from there is up to you. I can't tell what the purpose of the intent is so I will leave it at that.

As to the behavior supplied in you're comment, I think this is related to the need for an intent filter. Setting the category of an intent does not accomplish much unless you still include the filter to respond to that 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 effectivly listen for the categories you set programatically. That's why you will still need the corresponding category tags in your manifest to catch the intent.

like image 128
Rarw Avatar answered Nov 15 '22 19:11

Rarw


I came across a different way of doing this - essentially you cannot do exactly what you are asking, but you can disable/enable particular Activities of your app.

So you could have your Home app as disabled by default, then if a user wanted to use it, they could enable it programatically.

My info comes from the following answer by @Commonsware, which I'm duplicating here:


You can neither enable, disable, or create <intent-filter>s programmatically.

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


  • above answer originally by @Commonsware on a different thread
  • here is another useful answer by @mlc
like image 24
Richard Le Mesurier Avatar answered Nov 15 '22 17:11

Richard Le Mesurier