简体   繁体   中英

android.os.TransactionTooLargeException when executing queryIntentActivities

I am using a device with Android 5.0.1 and when execute the function:

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent,0);

An exception of type TransactionTooLargeException is thrown .... I cannot use the flag: MATCH_DEFAULT_ONLY because I can't be limited to only those activities that support the CATEGORY_DEFAULT. Looks that the problem is related with the amount of data returned, like many related issues that I found here at stackoverflow ... Is there a way to break this response? Or is there a query, equivalent, or a combination of flags that allow get the same results making more than one query? I am asking because is not clear to me the meaning of flag = 0 in documentation: https://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities(android.content.Intent , int)

May be I can query more than once, with different queries, and combine the results?

You could chunk your query by being a bit more specific with how you form your Intent , and do multiple queries rather than 1 lump sum query. Right now, you're querying basically every application (potentially multiple for each) on the device (since everything that can launch will have a android.app.action.Main action), which goes over your allocation of the ~1 MB shared buffer parcel limit causing the exception:

From the docs:

{ action=android.app.action.MAIN } matches all of the activities that can be used as top-level entry points into an application.

{ action=android.app.action.MAIN, category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.

So, adding a category narrows the search results. If you need everything of action type "android.app.action.Main", use that as the action, then iterate over an additional set of categories ex sudo code:

List<ResolveInfo> activities = new ArrayList<ResolveInfo>()

String[] categories = new String[] { android.app.category.LAUNCHER, additional... }

for(String category: categories){
   Intent mainIntent = new Intent(Intent.ACTION_MAIN)
   mainIntent.addCategory(category)
   List<ResolveInfo> matches = manager.queryIntentActivities(mainIntent, 0);  //You might want to use a better @ResolveInfoFlag to narrow your ResolveInfo data
   if(!matches.isEmpty()){
      activities.addAll(matches)
   }
}

You should also probably do a try catch, because when doing a MATCH_ALL, there's still no guarantee that the query won't be too large. There might be appropriate flags to help with this, but I'm not familiar with your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM