iapp代码 发送端
//iyu->mjava 调用链
//mian.iyu
call(null,"mjava","test.send")
//mjava:test.mjava
import android.app.Application;
import android.content.Intent;
public void send()
{
Intent intent = new Intent();
intent.setAction("test");//注意这里的action参数要和接收端一致
intent.putExtra("key","value");
Application app = activity.getApplication();
app.sendBroadcast(intent);
}
AIDE/Java 接受端
静态注册广播接收器:AndroidManifest.xml
<application>
<!--是否接受其他app的广播-->
<!--自定义广播接收器的类名称,此处需要创建一个单独的Receiver类-->
<receiver
android:exported="true"
android:name=".Receiver"
android:enabled="true">
<intent-filter>
<action android:name="test"></action>
</intent-filter>
</receiver>
</application>
//接收端
public class Receiver extends BroadcastReceiver
{
@Override
public void onReceiver(Context p1,Intent p2)
{
Bundle extras = p2.getExtras();
if(extras == null)
{
//没有附加数据
}
else
{
//根据key取出数据
String result = extras.getString("key");
//根据result进行处理
}
}
}
Unable to instantiate receiver报错的问题
出现这个问题的原因是,我是以内部类定义的MyReceiver类,需要声明为static:
这是AndroidManifest.xml中的注册配置: