How to get application category with package name

I have two or more applications with my specified category, for example

<category android:name="com.myapp.MY_CATEGORY"/> 

and I can get all packages in this category:

  final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory("com.myapp.MY_CATEGORY"); final List<ResolveInfo> pkgAppsList =getPackageManager().queryIntentActivities( mainIntent, 0); 

Now I want to track the installation and removal of the application for my category from the application, so I made a broadcast receiver that will give me the package name of the installed or remote application, but how can I go into the application category using the package name to determine if this is my application or not. can I make a broadcast receiver for certain categories of applications, or if not, how can I get a category from package_name.

+6
source share
1 answer

You can use a different approach. Define the meta information in the application manifest, rather than defining a category for your actions. Please note that deleted package information can no longer be obtained (if applications have not been removed using the DONT_DELETE_DATA flag)

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name" android:versionCode="1" android:versionName="1" > <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <meta-data android:name="meta-name" android:value="meta-value" /> </application> </manifest> 

Add a broadcast receiver to track package installations. Check the package meta-information to see what your values ​​already are.

 private BroadcastReceiver packageListener = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { Log.i(intent.getAction(), intent.getData().getEncodedSchemeSpecificPart()); //Currently being installed or deleted package String packageName = intent.getData().getEncodedSchemeSpecificPart(); Object value = null; try { ApplicationInfo appInfo = getPackageManager().getApplicationInfo(packageName,PackageManager.GET_META_DATA); //Get meta value if exits value = appInfo.metaData.get("meta-name"); } catch (NameNotFoundException e) { Log.e(TAG, "exception occured", e); } //check meta info if it is yours } } 

As you can see, any other applications can set your metadata in their manifest, so that there is no safe way to recognize your own applications. The best way is to check apk subscriptions if they are all signed with the same certificate.

 private BroadcastReceiver packageListener = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { Log.i(intent.getAction(), intent.getData().getEncodedSchemeSpecificPart()); String packageName = intent.getData().getEncodedSchemeSpecificPart(); Signature[] signatures = null; try { PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName,PackageManager.GET_SIGNATURES); signatures = packageInfo.signatures; } catch (NameNotFoundException e) { Log.e(TAG, "exception occured", e); } //check installed package signature if it matches } } 
-1
source

All Articles