关闭

Activity与Service通信之自定义接口

标签: android studioandroid自定义接口实现Service和Activ
97人阅读 评论(0) 收藏 举报
分类:
一、实现原理:
        自定义一个接口,该接口中有一个获取当前下载进度的空方法。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

效果图:


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:73593次
    • 积分:2003
    • 等级:
    • 排名:第19714名
    • 原创:120篇
    • 转载:57篇
    • 译文:0篇
    • 评论:14条
    最新评论