绑定service进行通信

本文深入探讨了Android中服务组件的创建及使用方式,通过自定义服务MyService与MainActivity交互实现数据绑定与同步,展示了如何启动、停止服务以及如何进行服务绑定与解除绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.yabushan.hello3;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {

    private boolean SERVICE_STATE=true;
    private String data="默认值";
    public MyService() {

    }
    @Override
    public IBinder onBind(Intent intent) {
        return new binder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("startCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    public class binder extends Binder{
        public void setData(String data){
            MyService.this.data=data;
        }
    }

@Override
public void onCreate() {
super.onCreate();
new Thread(){
@Override
public void run() {
super.run();
while(SERVICE_STATE){
System.out.println(data);
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}

  

package com.example.yabushan.hello3;

import android.app.Activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;



public class MainActivity extends Activity implements View.OnClickListener, ServiceConnection {

    private Intent intent;
    private MyService.binder binder;
    private TextView editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        editText= (TextView) findViewById(R.id.backText);
        intent=new Intent(MainActivity.this,MyService.class);
                findViewById(R.id.startService).setOnClickListener(this);
                findViewById(R.id.stopService).setOnClickListener(this);
                findViewById(R.id.bindService).setOnClickListener(this);
                findViewById(R.id.unbindService).setOnClickListener(this);
                findViewById(R.id.syncData).setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startService:
                startService(intent);break;
            case R.id.stopService:
                stopService(intent);break;
            case R.id.bindService:
                bindService(intent, this, Context.BIND_AUTO_CREATE);break;
            case R.id.unbindService:
                unbindService(this);break;
            case R.id.syncData://数据同步
                if(binder!=null)
                    binder.setData(editText.getText().toString());
                break;
        }

    }


    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        System.out.println("服务绑定成功");
        binder= (MyService.binder) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        System.out.println("服务解绑成功");

    }
}

  

binder 

转载于:https://www.cnblogs.com/yabushan/p/4984997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值