EventBus实现Activity与Service通信
来源:互联网 发布:上市的大数据公司 编辑:程序博客网 时间:2024/05/29 16:08
现在有如下需要:在一个Service启动一个定时线程,每隔一段时间从网络上获取一张图片,并将获取的图片更新到ActivityUI上。
(1)如果通过bindService()方法启动一个Service,虽然可以实现Service和Activity通信,但是绑定本地Service比较繁琐。
(2)如果通过startService()方法启动一个Service,通常情况下,Activity无法与Service通信。虽然可以借助BroadcastReceiver的帮组,但是BroadcastReceiver通过Intent只能传递少量的数据,无法传递较大的图片。
(3)为了解决上面的问题,可以通过EventBus来实现Service传递图片给Activity。具体代码如下:
MainActivity类
public class MainActivity extends Activity{ private ImageView imageView; private Button show; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); EventBus.getDefault().register(this); intent = new Intent(this , ChangeService.class); imageView = (ImageView) findViewById(R.id.picture); show = (Button) findViewById(R.id.show); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startService(intent); } }); } public void onEventMainThread(Bitmap bitmap) { if (bitmap != null) imageView.setImageBitmap(bitmap); } @Override protected void onDestroy() { super.onDestroy(); stopService(intent); EventBus.getDefault().unregister(this); }}
Service类
public class ChangeService extends Service{ private int imgId = 1; private LruCache<Integer , Bitmap> lruCache; private Bitmap showingImg; @Override public void onCreate() { super.onCreate(); initCache(); } @Override public int onStartCommand(final Intent intent, int flags, final int startId) { new Timer().schedule(new TimerTask() { @Override public void run() { if (imgId > 4) imgId = 1; if (getBitmapFromCache(imgId) != null) { showingImg = getBitmapFromCache(imgId); } else { showingImg = getBitmapFromUrl(imgId); if (showingImg != null) { putBitmapToCache(imgId , showingImg); } } EventBus.getDefault().post(showingImg); imgId++; } } , 0 , 3000); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void initCache() { int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 10; lruCache = new LruCache<Integer, Bitmap>(cacheSize) { @Override protected int sizeOf(Integer key, Bitmap value) { return value.getByteCount(); } }; } private Bitmap getBitmapFromUrl(Integer imgId) { InputStream is = null; Bitmap bitmap; try { URL url = new URL("http://192.168.1.100/pic" + imgId +".jpg"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(is); conn.disconnect(); return bitmap; } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } private Bitmap getBitmapFromCache(Integer imgId) { return lruCache.get(imgId); } private void putBitmapToCache(Integer imgId , Bitmap bitmap) { if (getBitmapFromCache(imgId) == null) { lruCache.put(imgId, bitmap); } }}
Service中使用了一个LruCache来缓存从网路上获取的图片,定时器每次需要获取Bitmap对象时,先从LruCache中获取,如果在缓存中没有找到图片,则从网络上获取,并将获取的图片放入缓存中。
0 0
- EventBus实现Activity与Service通信
- Activity与Service通信之EventBus
- Activity与Service通信
- Service与Activity通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Service与Activity通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Service与Activity通信
- Service与Activity进行通信
- Java集合之Map
- Android MessageQueue源码分析
- 工作中常用的XML解析Java代码实现
- Apache POI导出大数据量Excel文件导致Full GC的解决办法
- python中xrange和range的异同
- EventBus实现Activity与Service通信
- dfd
- 如何制作数字货币
- iOS实战——autoreleasepool
- 系统故障、程序失败和错误修正
- Map task 内部实现原理和运行机制
- 赤虹JSON模块 v1.0 麻雀虽小, 五脏俱全
- 怎么开发数字货币
- 关于cocoapods遇到的问题