• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

[Q] Hook X function use single instance of function Y for them

Search This thread

SnZ

Senior Member
Aug 14, 2011
359
45
Hello,

i've spent few days experimenting and i'm totally lost.
I will write some example scenario:

I have function_1 in which give different results on every run (if last call >30s new results, <30s will give old one) (the time is checked using another "Utils class")
I'm hooking 3 processes which use some_method_1
I need to give them exactly same results from my function_1


The problem is every hooked process run another instance (?) of function_1, so i get bad results.

I've experimented a lot with Intents, BroadcastReceiver etc.
Tried implemeting them in initZygote but the problem was i don't know why but i can't load there sqlite file
Code:
SQLiteDatabase db = SQLiteDatabase.openDatabase(PATH, null, 1);

loading of file mostly works only in some parts of handleLoadPackage.

Tried invoking file loading, when all was loaded but still error 14, i guess it's about that BroadcastReceiver was in initZygote , and to be strict it was:
Code:
findAndHookMethod("com.android.server.am.ActivityManagerService", null, "systemReady", Runnable.class, new XC_MethodHook()


I would be glad for any help, thanks in advance!
 

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,279
Actually each process has its own memory. During startup, there is only one process (well, on 32-bit ROMs that is) called Zygote. That's where Android initializes some stuff that is required by every app, and where initZygote() is called. Every time an app is started, it gets a copy of the Zygote process, including its memory at that time, but from that point on, the memory is independent. There is no sync between the app's memory and Zygote anymore, in neither direction, and no sync between apps.

If I understand you correctly, you store the result and the timestamp in variables. As explained above, that variable is not shared between processes due to Android's (or Linux') general architecture. That's what you need IPC for, i.e. using services, intents, files, ... I don't know what exactly you're trying to do and which solution would fit best. Using files is usually the easiest way, but they're not really suitable if you have lots of refreshes and you might run into racing conditions. With intents, you have to check whether you can find a good way to send back the result. And services are generally possible (used e.g. by XPrivacy), but harder to implement, especially under Lollipop.
 

SnZ

Senior Member
Aug 14, 2011
359
45
Actually each process has its own memory. During startup, there is only one process (well, on 32-bit ROMs that is) called Zygote. That's where Android initializes some stuff that is required by every app, and where initZygote() is called. Every time an app is started, it gets a copy of the Zygote process, including its memory at that time, but from that point on, the memory is independent. There is no sync between the app's memory and Zygote anymore, in neither direction, and no sync between apps.

If I understand you correctly, you store the result and the timestamp in variables. As explained above, that variable is not shared between processes due to Android's (or Linux') general architecture. That's what you need IPC for, i.e. using services, intents, files, ... I don't know what exactly you're trying to do and which solution would fit best. Using files is usually the easiest way, but they're not really suitable if you have lots of refreshes and you might run into racing conditions. With intents, you have to check whether you can find a good way to send back the result. And services are generally possible (used e.g. by XPrivacy), but harder to implement, especially under Lollipop.

IPC would be some good idea, thanks for hints Master, I appreciate your response!

//I aim only KK, don't plan to use LP