Android的 - 建立一个支持插件的应用程序 [英] Android - Build an application that supports add-ons

查看:121
本文介绍了Android的 - 建立一个支持插件的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,我可以使用反射加载DLL库在运行时。这使我能够建立一些附加在我的应用程序。

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

在Android上,有什么办法来执行类似的东西?我倒是想提出一个附加的是,在安装时,可以从我的Andr​​oid应用程序调用。

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.

我的第一个想法是建一个 APK 这个插件。然后,我的应用程序应该通过已安装的软件包回路,看是否加载项是present。

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.

在这之后,我又怎能装入包(和it's有活动),并用它在我的应用程序?另一个细节要考虑的是,附加需要访问主应用程序的同一个数据库。

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?

非常感谢!

推荐答案

使用PackageManager可以查询活动/等系统中的所有已安装的软件包(的APK)上。该ApiDemos样品code在Android SDK中的类使用这个 com.example.android.apis.ApiDemos

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例如:

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
}

清单片段:

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

本的ProtectionLevel标签将确保只有在图像的一部分,或者使用相同的密钥为主体的应用程序进行签名的APK可以拥有这个权限。

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.

有很多方法可以有插件访问你的数据库。远程服务或提供的接口是可行的。在应用程序,我有这样做,我的主要应用程序有一类称为DBManager已经不读了一堆/写入数据库。所以我只是用这个类在我的外部应用程序,它可以做所有的读/写,因为包使用相同的密钥签名。

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.

如果你不想使用相同的密钥签署包,可以考虑使用远程服务像一个样品code <一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html">RemoteService.java.看的Andr​​oid服务开发指南了解更多详情。我对Android的服务没有专家,所以从其他人的任何反馈可能会有所帮助。

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.

这篇关于Android的 - 建立一个支持插件的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆