我的应用叫做MyNiceApp。 MyNiceApp大多只是一个核心,在MainActivity onCreate
中加载一个名为coreView
的视图。 coreView
通过用户根据需要下载的其他插件的视图来填充。我定义了核心视图上的各个区域,这些区域可以通过MyNiceApp中的Interfaces插件填充。如何加载视图并将其从插件传递到coreView
?如何将视图添加到其他应用的应用
我被告知,RemoteViews是一个不错的选择,但我不知道如何实现它。还有什么其他选择?
RemoteViews是最好的方式吗?即使不是最好的方法,我也愿意尝试任何可行的方法。黑客会做。目前,任何可以提供这种功能的东西都已足够。稍后可以改进。
谢谢大家提前。
UPDATE
我想有他们的托管我的私人服务器上。他们将下载到称为/data/app/com.myniceapp.plugins
我想这将是更好的组织,如果我有下/data/app/com.myniceapp创建的文件夹专用文件夹。/plugins,然后让DexClassLoader为下载的插件抓取/data/app/com.myniceapp/plugins,然后我可以调用我的Class实现,并在运行时动态地将插件视图加载到核心视图。
临时更新
嗨@lelloman,和大家一样。我一直在努力让您的解决方案发挥作用,但迄今为止我一直未能成功。
我创建了一个名为的新项目测试视图。它有一个XML布局,我尝试膨胀并传送到核心观点如下:
package rev.ca.testview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import ca.rev.libs.core.MyViewCreator;
public class TestView implements MyViewCreator {
@Override
public View createMyView(Context context) {
LayoutInflater revInfl = LayoutInflater.from(context);
View toolBarItemsLL = revInfl.inflate(R.layout.layout, null, false);
Button button = (Button) toolBarItemsLL.findViewById(R.id.testButton);
return button;
}
}
但这不起作用。下面是它的其余部分:
在MainActivity认为,这是应该从插件获取的观点:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
String dexPath = "/data/app/rev.ca.testview";
String optimizedDirectory = this.getCacheDir().getAbsolutePath();
String libraryPath = null;
DexClassLoader dexClassLoader = new DexClassLoader(dexPath, optimizedDirectory, null, ClassLoader.getSystemClassLoader());
DexFile dexFile = null;
try {
dexFile = DexFile.loadDex(dexPath, File.createTempFile("opt", "dex", this.getCacheDir()).getPath(), 0);
for (Enumeration<String> classNames = dexFile.entries(); classNames.hasMoreElements();) {
String className = classNames.nextElement();
Class myClass = dexClassLoader.loadClass(className);
if (myClass.isAssignableFrom(MyViewCreator.class)) {
MyViewCreator creator = (MyViewCreator) myClass.getConstructor().newInstance();
View myView = creator.createMyView(this);
// add myView wherever you want
navigationView.addView(myView);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
抽屉布局实现发出更新
嗨@lelloman再次。我一直在尝试将您的解决方案实施到具有抽屉布局的主项目中。它突破final View toolBarItemsLL = revInfl.inflate(R.layout.activity_main, null, false);
。
为什么它不适用于抽屉布局。如果您将导航抽屉活动(android-plugins/MyNiceApps/app/src/main/
:新建 - >活动 - >导航抽屉活动)添加进来,那就是当它全部崩溃时。希望你能帮助。
这里是堆栈跟踪:
08-14 21:44:27.564 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f0b005e, entry index(94) is beyond type entryCount(9)
08-14 21:44:27.564 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f0b005e (t=10 e=94) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f0a002c, entry index(44) is beyond type entryCount(5)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f0a002c (t=9 e=44) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f060022, entry index(34) is beyond type entryCount(1)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f060022 (t=5 e=34) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore D/AndroidRuntime: Shutting down VM
08-14 21:44:27.566 13390-13390/rev.ca.revcore E/AndroidRuntime: FATAL EXCEPTION: main
Process: rev.ca.revcore, PID: 13390
android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Can't convert to ComplexColor: type=0x1
at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:879)
at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:756)
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:835)
at android.content.res.Resources.loadColorStateList(Resources.java:1002)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:531)
at android.widget.TextView.<init>(TextView.java:1076)
at android.widget.TextView.<init>(TextView.java:704)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at rev.ca.revbags.MyViewCreator.createView(MyViewCreator.java:24)
at rev.ca.revcore.rev_plugin_loader.RevPluginLoader.revLoadView(RevPluginLoader.java:36)
at rev.ca.revcore.RevCoreMainActivity$1.handleMessage(RevCoreMainActivity.java:21)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
用户如何下载插件? – lelloman
我正在考虑在我的私人服务器上托管插件。 @lelloman –
我的意思是,这些视图是什么格式,它们是实际的视图编译成Dex文件?定义视图结构的Json模型?你是否在你的应用中管理下载过程? – lelloman