My app has an embedded APK, I need to register broadcast receiver of inner apk from main apk. Inner APK is not to be installed on the system, I must load it dynamically. So, I'm using reflection to call a method of inner apk which has code to register the receiver. This receiver of inner apk should invoke for related broadcast.
I'm getting error while trying to register receiver. Is it even possible for a receiver to be registered in this way, and be fully functional?
Exception and code is given below, please guide me if there is something wrong with the code.
Error related log is given below
08-24 08:31:26.915: D/MainApp(1957): invoke method
08-24 08:31:26.955: D/InnerApp(1957): Register receiver
08-24 08:31:26.955: W/System.err(1957): java.lang.reflect.InvocationTargetException
08-24 08:31:26.965: W/System.err(1957): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 08:31:26.965: W/System.err(1957): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 08:31:26.965: W/System.err(1957): at com.example.ea_mainapp.MainApp.invokeService(MainApp.java:105)
08-24 08:31:26.965: W/System.err(1957): at com.example.ea_mainapp.MainApp.onCreate(MainApp.java:40)
08-24 08:31:26.965: W/System.err(1957): at android.app.Activity.performCreate(Activity.java:5231)
08-24 08:31:26.975: W/System.err(1957): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-24 08:31:26.975: W/System.err(1957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-24 08:31:26.975: W/System.err(1957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-24 08:31:26.975: W/System.err(1957): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-24 08:31:26.975: W/System.err(1957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-24 08:31:26.975: W/System.err(1957): at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 08:31:26.975: W/System.err(1957): at android.os.Looper.loop(Looper.java:136)
08-24 08:31:26.975: W/System.err(1957): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-24 08:31:26.975: W/System.err(1957): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 08:31:26.975: W/System.err(1957): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 08:31:26.975: W/System.err(1957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-24 08:31:26.975: W/System.err(1957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-24 08:31:26.975: W/System.err(1957): at dalvik.system.NativeStart.main(Native Method)
08-24 08:31:26.985: W/System.err(1957): Caused by: java.lang.NullPointerException
08-24 08:31:26.985: W/System.err(1957): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467)
08-24 08:31:26.985: W/System.err(1957): at com.example.ea_innerapp.InnerApp.register(InnerApp.java:50)
08-24 08:31:26.985: W/System.err(1957): ... 18 more`
Code of Main app is given below
// invoke method
Log.d(TAG,"invoke method");
final String apkFile =TARGET_BASE_PATH+"EA_innerApp.apk";
String className = "com.example.ea_innerapp.InnerApp";
String methodToInvoke = "register";
final File optimizedDexOutputPath = getDir("outdex", 0);
DexClassLoader dLoader = new DexClassLoader(apkFile,optimizedDexOutputPath.getAbsolutePath(),
null,ClassLoader.getSystemClassLoader().getParent());
try {
Class<?> loadedClass = dLoader.loadClass(className);
Object obj = (Object)loadedClass.newInstance();
@SuppressWarnings("rawtypes")
Class noparams[] = {};
Method m = loadedClass.getMethod(methodToInvoke, noparams);
Object oNoparams[] = {};
m.invoke(obj, oNoparams);
} catch (ClassNotFoundException e) {....
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}`
Code of invoked method is
Log.d(TAG, "Register receiver");
IntentFilter filter=new IntentFilter();
filter.addAction("com.example.ea_mainapp.i");
registerReceiver(obj_InnerReceiver,filter); `
Aucun commentaire:
Enregistrer un commentaire