diff --git a/replugin-sample/plugin/plugin-demo1/app/src/main/java/com/qihoo360/replugin/sample/demo1/MainActivity.java b/replugin-sample/plugin/plugin-demo1/app/src/main/java/com/qihoo360/replugin/sample/demo1/MainActivity.java index f842244..ac473db 100644 --- a/replugin-sample/plugin/plugin-demo1/app/src/main/java/com/qihoo360/replugin/sample/demo1/MainActivity.java +++ b/replugin-sample/plugin/plugin-demo1/app/src/main/java/com/qihoo360/replugin/sample/demo1/MainActivity.java @@ -17,8 +17,11 @@ package com.qihoo360.replugin.sample.demo1; import android.app.Activity; +import android.app.Dialog; import android.content.ContentValues; +import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.os.IBinder; @@ -41,6 +44,8 @@ import com.qihoo360.replugin.sample.demo1.activity.theme.ThemeDialogActivity; import com.qihoo360.replugin.sample.demo1.service.PluginDemoService1; import com.qihoo360.replugin.sample.demo2.IDemo2; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -179,6 +184,42 @@ public class MainActivity extends Activity { // ========= // Communication // ========= + mItems.add(new TestItem("ClassLoader: Use demo2's class", new View.OnClickListener() { + @Override + public void onClick(View v) { + // TODO 近期会添加RePlugin.fetchClass方法,无需catch一堆 + ClassLoader cl = RePlugin.fetchClassLoader("demo2"); + if (cl == null) { + Toast.makeText(v.getContext(), "Not install Demo2", Toast.LENGTH_SHORT).show(); + return; + } + + try { + Class clz = cl.loadClass("com.qihoo360.replugin.sample.demo2.MainApp"); + Method m = clz.getDeclaredMethod("helloFromDemo1", Context.class, String.class); + m.invoke(null, v.getContext(), "Demo1"); + } catch (ClassNotFoundException e) { + // 有可能Demo2根本没有这个类,直接返回 + Toast.makeText(v.getContext(), "MainApp not found", Toast.LENGTH_SHORT).show(); + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // 有可能没有这个方法 + Toast.makeText(v.getContext(), "helloFromDemo1() not found", Toast.LENGTH_SHORT).show(); + e.printStackTrace(); + } catch (IllegalAccessException e) { + // 也有可能不允许你访问 + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + })); + mItems.add(new TestItem("Fragment: Use demo2", new View.OnClickListener() { + @Override + public void onClick(View v) { + // TODO 志伟请搞定这里,谢谢! + } + })); mItems.add(new TestItem("Binder: Fast-Fetch", new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/replugin-sample/plugin/plugin-demo2/app/src/main/java/com/qihoo360/replugin/sample/demo2/MainApp.java b/replugin-sample/plugin/plugin-demo2/app/src/main/java/com/qihoo360/replugin/sample/demo2/MainApp.java index 7bd3077..670b5d3 100644 --- a/replugin-sample/plugin/plugin-demo2/app/src/main/java/com/qihoo360/replugin/sample/demo2/MainApp.java +++ b/replugin-sample/plugin/plugin-demo2/app/src/main/java/com/qihoo360/replugin/sample/demo2/MainApp.java @@ -17,6 +17,8 @@ package com.qihoo360.replugin.sample.demo2; import android.app.Application; +import android.content.Context; +import android.widget.Toast; import com.qihoo360.replugin.RePlugin; @@ -31,4 +33,8 @@ public class MainApp extends Application { RePlugin.registerPluginBinder("demo2test", new Demo2Impl()); } + + public static void helloFromDemo1(Context c, String text) { + Toast.makeText(c, "Demo2: hello! " + text, Toast.LENGTH_SHORT).show(); + } } diff --git a/replugin-sample/plugin/plugin-demo2/app/src/main/res/layout/from_demo1.xml b/replugin-sample/plugin/plugin-demo2/app/src/main/res/layout/from_demo1.xml new file mode 100644 index 0000000..dff6661 --- /dev/null +++ b/replugin-sample/plugin/plugin-demo2/app/src/main/res/layout/from_demo1.xml @@ -0,0 +1,11 @@ + + + + + +