Search code examples
javaandroidandroid-intentprogram-entry-point

Android Informing application through different process non-activity class


I'm currently developing an app which requires some adb commands

First i have a seperate module in application Module1, and there is com.mordred.xyz.Main class in there

public class Main {
    public static void main(String[] args) {
         System.out.println("mordred main class is instantiated");
         // rest of code
    }
}

I'm instantiating that Main class (com.mordred.xyz.Main) via adb (adb shell dalvikvm -cp com.mordred.MyApplication com.mordred.xyz.Main)

and i have an MainActivity in my application (com.mordred.MyApplication.MainActivity)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // MainActivity rest of code
    }
}

so the question is How can i inform the application.MainActivity that the "Main class is instantiated through adb" even if application is closed?

I tried to Context.sendIntent but it requires Context object, and also i cannot pass Context object from MainActivity nor other object (because i want Main class can be instantiated even if application is not running)

i dont want to use hidden apis (IActivityManager has broadcastIntent method but that class is hidden system api)

What can i do for it?

UPDATE:

eureka, probably i found a way,

i will set a hardcoded path in Main class and create a dummy file in there, and then will give a readable and writable permissions to everyone via File.setReadable() and then when Main class is instantiated, it will write something like "Main: 1" into that file, and then i will add a check into application's MainActivity class that will check that file if its "Main: 1" or not, ("Main: 1" means that the Main class is instantiated otherwise "Main: 0" means vice versa)


Solution

  • Ok i found a cleaner and nicer way to do it, yeah its reflection hack :P

    here is the function that i made to send Intents globally which can be called from everywhere

    i tested it and it works good, and also note that: it is for >Marshmallow, (because of main broadcastIntent() method's parameter types)

    public void sendGlobalIntent(Intent intent,String[] requiredPermissions) {
        try {
            Class<?> cActivityManagerNative = Class
                    .forName("android.app.ActivityManagerNative");
            Method mGetDefault = cActivityManagerNative.getMethod("getDefault");
            mGetDefault.setAccessible(true);
            Object oActivityManagerNative = mGetDefault.invoke(null);
            Method[] methods = cActivityManagerNative.getMethods();
            Method m = null;
            for (Method method : methods) {
                if (m.getName().equals("broadcastIntent")) {
                    m = method;
                }
            }
            if (m == null) {
                return;
            }
            m.setAccessible(true);
            m.invoke(oActivityManagerNative, null, intent, null, null, 0, null,
                    null, requiredPermissions, -1, null, true, false, android.os.Process.myUid() / 100000);
    
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }