[英]Android call method from another app
I have 2 android apps. 我有2个Android应用程序。 Both are installed on the phone.
两者都安装在手机上。 Lets say the package name for the two are com.android.test1 and com.android.test2.
让我们说两者的包名是com.android.test1和com.android.test2。 How can i call the method
Main2method()
from the test1.Main class ? 如何从test1.Main类调用
Main2method()
方法?
Class for test1: 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: 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..
}
}
Maybe you can broadcast an Intent to call it. 也许你可以广播一个Intent来调用它。
Intent it = new Intent("com.android.test2.Main2method");
context.sendBroadcast(it)
Make a BroadcastReceiver
in com.android.test2.Main2
to receive the broadcast: 在
com.android.test2.Main2
一个BroadcastReceiver
来接收广播:
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
: 在
Main1
类的onCreate
方法中注册接收器:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
receiver = new ActionReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.test2.Main2method");
registerReceiver(receiver, filter);
...
}
If you want to send callbacks from app1 to app2: 如果要将回调从app1发送到app2:
Intent
with data from app1. Intent
与来自app1的数据一起投入。 (take look at PendingIntent
). PendingIntent
)。 BroadcastReceiver
which will handle your app1's Intents
. BroadcastReceiver
,它将处理你的app1的Intents
。 onReceive
method (in app2) will be called each time when your Intent will be thrown by app1 and catched by app2. onReceive
方法(在app2中)。 (put your logics there) in order to call method between different application you will need to use, Intent 为了在不同的应用程序之间调用方法,你需要使用Intent
also you will need intent-filter and BroadcastReceiver 你还需要intent-filter和BroadcastReceiver
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.
相反,您必须从另一个活动调用一个活动并使用Intent过滤器获取结果。
These links might help you 这些链接可能会帮助您
http://www.vogella.com/articles/AndroidIntent/article.html http://www.vogella.com/articles/AndroidIntent/article.html
http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.