Android custom categories

﹥>﹥吖頭↗ 提交于 2020-01-13 06:28:47

问题


I have a view as a main screen of the application which contains the available application's actions as icon+text pairs ( desktop like).

I want to find out programatically what are the activities defined ONLY in my AndroidManifest.xml

Suppose I have :

< activity android:name="example.mainActivity" android:label="mainActivity">  
    < intent-filter>  
        < action android:name="android.intent.action.MAIN" />  
        < category android:name="android.intent.category.LAUNCHER" />  
    < /intent-filter>  
< /activity>  
< activity android:name="example.activity1" android:label="Activity1">  
    < intent-filter>  
        < action android:name="android.intent.action.VIEW" />  
        < category android:name="example.custom.ACTIVITY" />  
    < /intent-filter>  
< /activity>  
< activity android:name="example.activity2" android:label="Activity2">  
    < intent-filter>  
        < action android:name="android.intent.action.VIEW" />  
        < category android:name="example.custom.ACTIVITY" />  
    < /intent-filter>  
< /activity>  

I want that in the mainActivity to dinamically read Activity1 and Activity2 because when i add Activity3 for example it will be automatically read.

Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("example.custom.ACTIVITY"); List resolves = getPackageManager().queryIntentActivities(intent, 0);

In the resolves list i expect to have the two defined activities so i can get their label and image and create the icon for the desktop

I thought that this could be done by defining a custom category, example.custom.ACTIVITY, and in the mainActivity use the packageManager.queryIntentActivities(Intent intent, int flags) but it doesn't seem to be working.

I really would like to code it to dinamically discover the installed activities in my application. Do you have any ideas on how to do this? Thank you


回答1:


In my app I defined the actions as "android.intent.action.MAIN" (not "android.intent.action.VIEW"), and the retrieval works as you expected:

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory("com.example.ACTIVITY");
PackageManager pm = getPackageManager(); 
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); 

p.s., i used "com.example" since "example" is not a good namespace example, as the package declaration in your manifest must have at least two segments (see here)



来源:https://stackoverflow.com/questions/2681053/android-custom-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!