本地服务和Activity交互的示例

具体做法是,服务类需要增加接口,比如ICountService,另外,服务类需要有一个内部类,这样可以方便访问外部类的封装数据,这个内部类 需要继承Binder类并实现ICountService接口。还有,就是要实现Service的onBind方法,不能只传回一个null了。

这是新建立的接口代码:

public interface ICountService {
    public abstract int getCount();
}
 
修改后的CountService代码:
package com.easymorse;

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

public class CountService extends Service implements ICountService {

    private boolean threadDisable;

    private int count;

    private ServiceBinder serviceBinder = new ServiceBinder();

    public class ServiceBinder extends Binder implements ICountService{
        @Override
        public int getCount() {
            return count;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return serviceBinder;
    }

    @Override
    public void onCreate() {
        super .onCreate();
        new Thread( new Runnable() {

            @Override
            public void run() {
                while ( ! threadDisable) {
                    try {
                        Thread.sleep( 1000 );
                    } catch (InterruptedException e) {
                    }
                    count ++ ;
                    Log.v( " CountService " , " Count is " + count);
                }
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super .onDestroy();
        this .threadDisable = true ;
        Log.v( " CountService " , " on destroy " );
    }

    /* (non-Javadoc)
     * @see com.easymorse.ICountService#getCount()
     */ 
    public int getCount() {
        return count;
    }

}

需要通过ServiceConnection的内部类实现来连接Service和Activity。


public class LocalServiceDemoActivity extends Activity {

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            countService = (ICountService) service;
            Log.v( " CountService " , " on serivce connected, count is " 
                    + countService.getCount());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            countService = null ;
        }

    };

    private ICountService countService;

    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this .bindService( new Intent( " com.easymorse.CountService " ),
                this .serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
          this .unbindService(serviceConnection);        
          super .onDestroy();       //注意先后 
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值