Android Create an application that supports add-ons.

In .NET, I can use reflection to load the DLL at runtime. This allows me to add some additions to my applications.

Is there a way on Android to do something like this? I would like to make an add-in that during installation can be called from my Android application.

My first idea is to create another APK for this add-on. Then my application should go through the installed packages to find out if the add-in is present.

After that, how can I download this package (and the actions that it has) and use it in my application? Another detail to consider is that add-on needs to access the same database of the main application.

Does anyone know how I can accomplish this?

Thanks a lot!

0
source share
1 answer

Using the PackageManager, you can request / etc actions on the system in all installed packages (APKs). Sample ApiDemos code in the Android SDK uses this in the class com.example.android.apis.ApiDemos .

Code example:

 void findAddOnActivities() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory("myPackage.intent.category.ADDON"); PackageManager pm = getPackageManager(); List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); // list contains all activities that match your filters in mainIntent } 

Fragment Manifest:

  <activity android:label="My external Add-on" android:name=".addons.TestAddon" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="myPackage.intent.category.ADDON" /> </intent-filter> </activity> 

To access the database and control access / security modification: Declare your own permissions in the manifest:

 <permission android:name="com.mycompany.myapp.database.permission.READ" android:label="@string/read_permission" android:protectionLevel="signatureOrSystem"></permission> <permission android:name="com.mycompany.myapp.database.permission.WRITE" android:label="@string/write_permission" android:protectionLevel="signatureOrSystem"></permission> 

The protectionLevel tag will ensure that only this can be provided only by APKs that are part of the image or signed with the same key as the main application.

There are many ways an addon can access your database. The remote service or interface will work. This happens in my application, my main application has a DBManager class that already does a bunch of read / write to the database. Therefore, I just use this class in my external application, and it can do all the reads / writes since the packages are signed with the same key.

If you do not want to use the same key to sign packages, consider using a remote service, for example, in the RemoteService.java code example. Check out the Android Developers Guide for more details. I am not an expert on Android services, so any feedback from others is likely to be helpful.

+8
source

Source: https://habr.com/ru/post/977648/


All Articles