android 模块间跳转,Android组件化之不同模块间 交互(activity互相跳转,数据互相传递,互相调用函数)...

/**

* @author gaolei

* @Description:供Antivirus模块调用的类

*

*/

public class GetSyncData implements IDataDelegate {

@Override

public Bundle getData(Bundle args, Object... extras) throws DelegateException {

if (extras.length > 0 && extras[0] != null) {

if (extras[0] instanceof Context) {

final Context context = (Context) extras[0];

new Thread() {

public void run() {

try {

new Thread() {

public void run() {

try {

//模拟耗时操作,在这个线程里可以做耗时操作,然后把数据通过广播传出去

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

Bundle bundle = new Bundle();

bundle.putString("result", "这是一条异步获取的数据从Boost模块");

Intent intent = new Intent("com.example.getdata");

intent.putExtras(bundle);

context.sendBroadcast(intent);

}

}.start();

} catch (Throwable e) {

}

}

}.start();

}

}

return null;

}

}

public class AntivirusActivity extends Activity {

TextView text;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_antivirus);

text=findViewById(R.id.text);

IntentFilter filter=new IntentFilter("com.example.getdata");

DataReceiver dataReceiver=new DataReceiver();

registerReceiver(dataReceiver,filter);

}

public void getDataFromOtherModule(View view) {

Bundle bundle;

try {

bundle = new Bundle();

bundle.putString("gaolei", "beauty");

Bundle bundle1= ModuleDelegate.getInstance().getData(BoostDelegateConsts.FACTORY, BoostDelegateConsts.DataCode.getTotalMemoryByte,bundle,AntivirusActivity.this);

String result=bundle1.getString("result");

text.setText(result);

} catch (Exception e) {

e.printStackTrace();

}

}

public void getSyncDataFromOtherModule(View view) {

Bundle bundle;

try {

bundle = new Bundle();

//这个地方是调用Boost模块,获取异步数据

Bundle bundle1= ModuleDelegate.getInstance().getData(BoostDelegateConsts.FACTORY, BoostDelegateConsts.DataCode.getSyncData,bundle,AntivirusActivity.this);

} catch (Exception e) {

e.printStackTrace();

}

}

public class DataReceiver extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

Bundle bundle=intent.getExtras();

String data=bundle.getString("result");

text.setText(data);

}

}

}

三、可以使用第三方框架EventBus,可参考:

http://www.voidcn.com/article/p-ctocwtph-kp.html