activity和service通信

来源:互联网 发布:unity3d 粒子特效爆炸 编辑:程序博客网 时间:2024/05/18 03:35

一、activity和service通信

1.直接启动service的方式,但是这样会暂时和service失去联系。

Intent intent=new Intent("aaa");
intent.putExtra("data","helloData");
startService(intent);

2.用绑定的方式来启动service,这样service会返回一个binder,通过binder,两者就可以交互了。



二、service和activity通信

1.通过广播的形式。

2.通过在service重启一个activity,但是因为这个不是在activity中发出所有要做如下设置:

    Intent intentSend=new Intent(Constants.ACTION_STATUS);
    intentSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intentSend.putExtra("status","end");
    startActivity(intentSend);

    但是此时会引发一个问题,多次startActivity会导致很多的activity实现运行,这肯定不是我们要德,我只要
   一个Activity就可,此时,我们要在androidMainfest里面对这个activity的launchMode设置singleInstance。

0 0
原创粉丝点击