Activity与Service通信之自定义接口

一、实现原理:
        自定义一个接口,该接口中有一个获取当前下载进度的空方法。Server端用一个类继承自Binder并实现该接口,覆写了其中获取当前下载进度的方法。Client端通过ServiceConnection获取到该类的对象,从而能够使用该获取当前下载进度的方法,最终实现实时交互。
二、实现步骤:
1、自定一个接口:
public interface ICountService {
int getCurrentLoad();
}
2、定义一个类继承Service,其中要:
     (1) 自定义Binder实现上面的接口:
/**
* 自定义 Binder
*/
public class ServiceBinder extends Binder implements ICountService {

@Override
public int getCurrentLoad() {
return temp;
}

}
    (2)返回自定义Binder对象
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ServiceBinder();
}
3、Client端使用bindService()绑定Server端,通过public void onServiceConnected(ComponentName name, IBinder service)方法获取服务端的自定义Binder对象,进而可以使用计时器不断的获取进度
/** 服务练级监听*/
private ServiceConnection serConn = new ServiceConnection() {

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

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serviceBinder = (CustomInfaceService.ServiceBinder) service;
if (serviceBinder != null){
timer.schedule(new MyTimerTask(), 0, 1000);
}
}
};
三、核心代码:
Service:
importandroid.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
import testndk.testservice.interf.ICountService;
/**
* <h3>Description</h3>
* TODO
* <h3>Author</h3> luzhenbang
* <h3>Date</h3> 2016/6/1 9:44
* <h3>Copyright</h3> Copyright (c)2016 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
*/
public class CustomInfaceService extends Service {

/** 计时器*/
private Timer timer;
private int temp = 0;

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 21:
if (temp == 100){
temp = 0;
}else {
temp ++ ;
}
break;
}
}
};

@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ServiceBinder();
}
@Override
public void onCreate() {
timer = new Timer();
timer.schedule(new MyTimerTask(), 0, 1000);
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
/**计时器*/
class MyTimerTask extends TimerTask {
@Override
public void run() {
Message message = handler.obtainMessage();
message.what = 21;
handler.sendMessage(message);
}
}
/**
* 自定义 Binder
*/
public class ServiceBinder extends Binder implements ICountService {

@Override
public int getCurrentLoad() {
return temp;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null){
timer.cancel();
}
}
}
Activity:
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.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import testndk.testservice.service.CustomInfaceService;

public class CustomInActivity extends Activity {
private boolean mIsBind;
private Button startBtn = null;
private Button stopBtn = null;
private TextView mTextView = null;
private Intent intent = null;
private ProgressBar mProgressBar = null;
private CustomInfaceService.ServiceBinder serviceBinder;

/** 计时器*/
private Timer timer;

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 21:
int curLoad = serviceBinder.getCurrentLoad();
mTextView.setText(curLoad+"%");
mProgressBar.setProgress(curLoad);
break;
}
}
};

/** 服务练级监听*/
private ServiceConnection serConn = new ServiceConnection() {

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

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serviceBinder = (CustomInfaceService.ServiceBinder) service;
if (serviceBinder != null){
timer.schedule(new MyTimerTask(), 0, 1000);
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new Timer();

intent = new Intent(this,CustomInfaceService.class);
mProgressBar = (ProgressBar)findViewById(R.id.myProgressBar);
mProgressBar.setMax(100);
mTextView = (TextView)findViewById(R.id.loading_Tv);
startBtn = (Button)findViewById(R.id.start_Btn);
stopBtn = (Button)findViewById(R.id.stop_Btn);
startBtn.setOnClickListener(new ButtonClickListener());
stopBtn.setOnClickListener(new ButtonClickListener());

}
class ButtonClickListener implements OnClickListener {

@Override
public void onClick(View v) {
if (startBtn == v) {
mIsBind = bindService(intent, serConn, Context.BIND_AUTO_CREATE);//if bind success return true
} else if (stopBtn == v) {
if(mIsBind){
unbindService(serConn);
// timer.cancel();
mIsBind = false;
}
}
}

}


/**计时器*/
class MyTimerTask extends TimerTask {
@Override
public void run() {
Message message = handler.obtainMessage();
message.what = 21;
handler.sendMessage(message);
}
}


@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null){
timer.cancel();
}
if (mIsBind){
unbindService(serConn);
}
}
}

源码下载地址:

http://download.csdn.net/detail/lu1024188315/9537991

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值