关闭

Activity与Service通信之EventBus

标签: android studioandroidService和Activity通信EventBus
295人阅读 评论(0) 收藏 举报
分类:
一、导包:
compile 'org.simple:androideventbus:1.0.5'
二、在Activity和Service分别注册EventBus
//注册EventBus
eventBus = EventBus.getDefault();
eventBus.register(this);
在onDestroy()注销EventBus
@Override
protected void onDestroy() {
super.onDestroy();
eventBus.unregister(this);
}
三、启动计时发消息:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer = new Timer();
timer.schedule(new MyTimerTask(), 0, 1000);
return START_STICKY;
}

/**计时器*/
class MyTimerTask extends TimerTask {
@Override
public void run() {
Message message = handler.obtainMessage();
message.what = 21;
handler.sendMessage(message);
}
}
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 ++ ;
}
eventBus.post(temp,EVNET_TAG);
break;
}
}
};
三、核心代码:
Service:

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import org.simple.eventbus.EventBus;

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

/**
* <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 EventService extends Service {

private int temp = 0;
public static final String EVNET_TAG = "EventService";

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 ++ ;
}
eventBus.post(temp,EVNET_TAG);
break;
}
}
};

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


@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onCreate() {
super.onCreate();
eventBus = EventBus.getDefault();
eventBus.register(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer = new Timer();
timer.schedule(new MyTimerTask(), 0, 1000);
return START_STICKY;
}

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

@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
eventBus.unregister(this);
}
}
Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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 org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import testndk.testservice.service.EventService;
public class EventActivity 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 Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 21:
int curLoad = msg.arg1;
mTextView.setText(curLoad+"%");
mProgressBar.setProgress(curLoad);
break;

default:
break;
}
}
};
private EventBus eventBus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
eventBus = EventBus.getDefault();
eventBus.register(this);

intent = new Intent(this,EventService.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) {
startService(intent);
} else if (stopBtn == v) {
if(mIsBind){
stopService(intent);
mIsBind = false;
}
}
}
}

/**
* 接收服务端的数据
* @param temp
*/
@Subscriber(tag = EventService.EVNET_TAG)
private void getEventData(int temp){
Message message = mHandler.obtainMessage();
message.what = 21;
message.arg1 = temp;
mHandler.sendMessage(message);
}

@Override
protected void onDestroy() {
super.onDestroy();
eventBus.unregister(this);
}
}
注意:不要忘记在配置文件中注册服务。

源码下载地址:

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

效果图:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:86697次
    • 积分:2225
    • 等级:
    • 排名:第17698名
    • 原创:127篇
    • 转载:66篇
    • 译文:0篇
    • 评论:15条
    博客专栏
    最新评论