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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值