Android - Build an application that supports add-ons

给你一囗甜甜゛ 提交于 2019-12-18 12:43:09

问题


On .NET, I can use "reflection" to load a DLL library at runtime. This allows me to build some add-on to my applications.

On Android, there is any way to perform something like that? I´d like to make an add-on that, when installed, can be called from my android app.

My first idea is to build another APK to this add-on. Then, my application should loop through the installed packages to see if the add-on is present.

After that, how could I load that package (and the activities that it´s have) and use it on my application? Another detail to consider is that the add-on needs to access the same database of the main application.

Does anybody knows how could I perform this?

Thanks a lot!


回答1:


Using PackageManager you can query Activities/etc on the system in all installed packages(APKs). The ApiDemos sample 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
}

Manifest snippet:

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

For Database access and managing access/modify security: Declare your own permissions in your 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 make sure that only APKs that are part of the image or are signed with the same key as the main app can be granted that permission.

There are many ways to have the addon access your database. A remote service or providing an interface would work. In the app I have that does this, my main app has a class called DBManager that already does a bunch of read/writes to the database. So I just use that class in my external App and it can do all the reads/writes since the packages are signed with the same key.

If you don't want to use the same key to sign packages, consider using a remote service like the one in the sample code RemoteService.java. Look at the android service dev guide for more details. I'm no expert on android Services, so any feedback from others would probably be helpful.



来源:https://stackoverflow.com/questions/9236803/android-build-an-application-that-supports-add-ons

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