Add the sample code: Use another plugin for the class, Fragment

This commit is contained in:
zhangjiongxuan 2017-07-05 23:04:39 +08:00
parent 403c4b9125
commit 845b1c97a1
3 changed files with 58 additions and 0 deletions

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello! Demo2" />
</LinearLayout>