Activity 与 Service 之间的消息传递

BgService代码

复制代码
public class BgService extends Service {
    public static final String TAG = "BgService";
    private BgBinder binder;

    @Override
    public void onCreate() {
        super.onCreate();
        binder = new BgBinder();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind() executed");
        return binder;
    }

    class BgBinder extends Binder {
        public void doWork() {
            Log.d(TAG, "doWork() executed");
        }
    }
}
复制代码

Activity 代码

复制代码
public class MyActivity extends AppCompatActivity {
    private ServiceConnection bgServiceConnection;
    private BgService.BgBinder bgBinder;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        bgServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                bgBinder = (BgService.BgBinder)service;
                bgBinder.doWork(); // 异步回调必须在这里调用
                Log.d("MyActivity", "onServiceConnected");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                bgBinder = null;
                Log.d("MyActivity", "onServiceDisconnected");
            }
        };
        ...
    }

    public void startBg(View view) {
        Intent intent = new Intent(this, BgService.class);
        startService(intent);
    }

    public void stopBg(View view) {
        Intent intent = new Intent(this, BgService.class);
        stopService(intent);
    }

    public void bindBg(View view) {
        Intent intent = new Intent(this, BgService.class);
        bindService(intent, bgServiceConnection, BIND_AUTO_CREATE);
    }

    public void unbindBg(View view) {
        unbindService(bgServiceConnection);
    }
}
复制代码


其中
startService 调用onStartCommand, 如果service未create, 会在调用之前, 调用onCreate
stopService 会检查service被引用的情况, 如果没有其他引用, 则调用onDestroy

bindService 会调用onBind(之后在异步返回中执行onServiceConnected), 如果service未create, 会在调用之前, 调用onCreate. 注意, 不会调用onStartCommand
unbindService 会检查service被引用的情况, 如果没有其他引用, 则调用onDestroy,

unbindService与stopService的区别: stopService可多次调用, 不会抛异常, unbindService在执行后如果再调用会抛异常, 不处理则应用崩溃

onServiceDisconnected为什么没被调用? 这个只有在service因异常而断开连接的时候,这个方法才会被调用 This is called when the connection with the service has been unexpectedly disconnected -- that is, its process crashed. Because it is running in our same process, we should never see this happen.



posted on   Milton  阅读(1635)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示