希望这对你有用,
在 App1 中:调用 App2 的广播接收器(我的广播接收器“MyBroadCastReceiver”的名称)。
您可以将此方法放置在任何 Button onClick() 中或根据您的要求。
private void getAnotherAppMethod(){
Intent intent = new Intent("Updated");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
// for Example, here packageName of app2 is "com.app2.example" and its class name with packageName can be like "com.app2.example.yourBroadCastRecevier"
intent.setComponent(new ComponentName("package name of app2","package.yourbroadcastreciverName in app2"));
getContext().sendBroadcast(intent);
}
在 App2 中:位于 App2 中的被称为广播接收器
public class MyBroadCastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null)
{
String sIntentAction = intent.getAction();
if (sIntentAction != null && sIntentAction.equals("youActionName"))
{
Toast.makeText(context, "Hello From App2", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context,"Something went wrong",Toast.LENGTH_SHORT).show();
}
}
}
}
在 App2: AndroidManifest.xml 文件中,在您的应用程序标签
中添加以下代码
<receiver
android:name=".MyBroadCastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="youActionName" />
</intent-filter>
</receiver>