11

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!

Carlos Pereira
  • 1,914
  • 6
  • 24
  • 35

2 Answers2

6

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.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • Hello @Rarw! Thank you for answering. Here is what I did: 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); HOWEVER it didn't work. If I put these categories in my Manifest, they work (you can't leave my app pressing home button). When I do it like you said, it doesn't work. You can still leave my app pressing home button. – Carlos Pereira May 08 '13 at 19:44
  • I added an explanation as to why. Please see above – Rarw May 08 '13 at 20:18
  • Hello again Rarw! Thank you for your explanation again! So, is it possible to do everything programmatically? I mean, if I have two users, one can leave the app by pressing home button and another one cannot do it. Is it possible also set intent filters programmatically? – Carlos Pereira May 08 '13 at 21:01
  • You can create an intent filter programatically. Honestly what it sounds like you're getting at here is whats called a `BroadcastReceiver`. [Here's a link](http://www.slideshare.net/CodeAndroid/android-intent-intent-filter-broadcast-receivers) to some slides explaining how to register and `IntentFilter` in the java code. I will give one word of caution though, based on my understanding of the android docs its bad form to override the home button. Unlike the back button which you can customize home was not intended to be caught and rerouted. But you know your app better than me. – Rarw May 09 '13 at 00:49
  • Thank you for the advice Rarw! I really need to override the home button because my app is supposed to be a kiosk app. I will check the link you sent. Thank you again! – Carlos Pereira May 09 '13 at 13:00
4

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>.


Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255