Android call method from another app
Asked Answered
P

5

15

I have 2 android apps. Both are installed on the phone. Lets say the package name for the two are com.android.test1 and com.android.test2. How can i call the method Main2method() from the test1.Main class ?

Class for test1:

package com.android.test1;
import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Class for test2:

package com.android.test2;
import android.app.Activity;
import android.os.Bundle;

public class Main2 extends Activity {  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public static void Main2method() {
        //do something..
    }
}
Plane answered 9/9, 2012 at 12:30 Comment(1)
Need to create ActionReceiver class in test1 (app1) also?Kono
M
16

Maybe you can broadcast an Intent to call it.

Intent it = new Intent("com.android.test2.Main2method");
context.sendBroadcast(it)

Make a BroadcastReceiver in com.android.test2.Main2 to receive the broadcast:

public class ActionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.android.test2.Main2method".equalsIgnoreCase(intent.getAction())) {
            Main2method();
        } 
    }
}

Register the receiver in onCreate method of class Main1:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    receiver = new ActionReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.android.test2.Main2method");
    registerReceiver(receiver, filter);
    ...
}
Mccall answered 9/9, 2012 at 13:11 Comment(3)
Question: Do I have to have the ActionReceiver Class in both apps ?Marcosmarcotte
How to call ` Main2method();` from class Main2 in test2 app in another class ActionReceiver normally it does not recognize it?Simplex
Need to create ActionReceiver class in test1 (app1) also?Kono
E
3

in order to call method between different application you will need to use, Intent

also you will need intent-filter and BroadcastReceiver

Ecclesia answered 9/9, 2012 at 12:35 Comment(0)
M
3

If you want to send callbacks from app1 to app2:

  1. you should throw your own Intent with data from app1. (take look at PendingIntent).
  2. into yout app2 you should register BroadcastReceiver which will handle your app1's Intents.
  3. broadcastreceiver's onReceive method (in app2) will be called each time when your Intent will be thrown by app1 and catched by app2. (put your logics there)
Mozellemozes answered 9/9, 2012 at 13:27 Comment(0)
L
0

You cannot directly call a method of one app from another app. Instead, you have to invoke one activity from another and fetch result using Intent filters.

These links might help you

http://www.vogella.com/articles/AndroidIntent/article.html

http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html

Laborer answered 9/9, 2012 at 13:24 Comment(0)
W
0

Register the receiver in onCreate method of class Main1: is not correct, instead you must register in onCreate method of class Main2

Weathersby answered 4/5, 2023 at 22:35 Comment(1)
This answer appears to be a comment on another answer. However you have also made a statement on how to fix the issue. Both without any expansion or explanation. Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Biel

© 2022 - 2024 — McMap. All rights reserved.