使用资源动态加载的apk [英] Dynamically load apk that uses resources

查看:295
本文介绍了使用资源动态加载的apk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的资源,以显示在状态栏的通知service.apk。

I have service.apk that is using resources to show a notification in the status bar.

我动态加载正在使用这些资源,但资源的通知消失不知何故类,我得到这个错误:

I am dynamically loading the class that is using these resources but then the resources for the notifications disappear somehow, and I'm getting this error:

W /的ResourceType(1142):获得资源数量0x7f020001值时,没有已知的包
W / StatusBarIconView(1142):图标不在2130837505发现:7f020001
W / StatusBarIconView(1142):无插槽的Andr​​oid / 0x20的图标
W / ActivityManager(941):crashApplication:试图崩溃的自我!
D / NotificationService(941):onNotification错误PKG =机器人标签= NULL ID = 32;将crashApplication(UID = 1000,PID = 941)
W /状态栏(1142):removeNotification未知键:android.os.BinderProxy@41b166b8

在code动态加载:

PathClassLoader classLoader =
                new PathClassLoader("system/app/ServiceImpl.apk",
                               ClassLoader.getSystemClassLoader());
              Class someClass =
            classLoader.loadClass("com.bla.ServiceImpl");
              Constructor<Class> ctor = someClass.getConstructor(Context.class);
              Object someObject = ctor.newInstance(context);

我想也许我加载APK的方式是错误的?也许我应该添加一个路径的资源?

I'm thinking maybe the way I'm loading the apk is wrong? Maybe I should add a path for the resources?

任何帮助将非常AP preciated!

Any Help would be very much appreciated!

推荐答案

您可以参考下面的开源项目:
https://github.com/singwhatiwanna/dynamic-load -apk / BLOB /主/ README-en.md

you can refer to the following open source project: https://github.com/singwhatiwanna/dynamic-load-apk/blob/master/README-en.md

下面是重点思考:
第一,APK可以通过主机Android应用程序加载,忽略了它是在SD卡或者在系统目录下。但如何才能做到这一点?需要两个问题需要解决:资源和活动的lifecircle。以上项目可以解决此两个问题,现在,使用的资源是可能的,使用DexClassLoader并提供新的AssetManager能满足您的需求动态加载APK。

following is the key thinking: first, apk can be loaded by host android application, ignored that it is on sdcard or on system directory. but how can we do this? two problems need to be resolved: resource and activity's lifecircle. the above project resolves this two problems, now, Dynamically load apk that uses resources is possible, use DexClassLoader and provide new AssetManager can satisfy your demand.

protected void loadResources() {
    try {
        AssetManager assetManager = AssetManager.class.newInstance();
        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
        addAssetPath.invoke(assetManager, mDexPath);
        mAssetManager = assetManager;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Resources superRes = super.getResources();
    mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(),
            superRes.getConfiguration());
    mTheme = mResources.newTheme();
    mTheme.setTo(super.getTheme());
}

然后,资源可以由R标识符访问,使用的getResource。

then, resources can be visited by R identifer, use getResource.

这篇关于使用资源动态加载的apk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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