package com.example.jikangwang.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; import butterknife.BindView;
import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.button)
Button button; @BindView(R.id.progressbar)
ProgressBar progressBar;
Handler mHandler; volatile boolean flag=true;
Object object=new Object(); ServiceConnection serviceConnection; MyService myService;
private int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ButterKnife.bind(this); serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService=((MyService.MyBinder) service).getService();
} @Override
public void onServiceDisconnected(ComponentName name) { }
}; final Intent intent=new Intent(MainActivity.this,MyService.class); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myService.startDownLoad(); listenProgress();
}
}); MyThread thread=new MyThread(); } public void listenProgress(){
new Thread(new Runnable() { @Override
public void run() {
while(progress < myService.MAX_PROGRESS){
progress = myService.getProgress();
progressBar.setProgress(progress);
System.out.println("progress="+progress);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
}).start();
} public class MyThread extends Thread{
@Override
public void run() {
super.run();
}
} }
package com.example.jikangwang.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable; public class MyService extends Service { /**
* 进度条的最大值
*/
public static final int MAX_PROGRESS = 100;
/**
* 进度条的进度值
*/
private int progress = 0; /**
* 增加get()方法,供Activity调用
* @return 下载进度
*/
public int getProgress() {
return progress;
} @Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId); } public void startDownLoad(){
new Thread(new Runnable() { @Override
public void run() {
while(progress < MAX_PROGRESS){
progress += 5;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}).start();
} public class MyBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
}

bindservice与Activity通信的更多相关文章

  1. Android中Service与多个Activity通信

    由于项目需要,我们有时候需要在service中处理耗时操作,然后将结果发送给activity以更新状态.通常情况下,我们只需要在一个service与一个activity之间通信,通常这种情况下,我们使 ...

  2. 关于Fragment与Fragment、Activity通信的四种方式

    一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...

  3. Service与Activity通信 回调方式***

    要实现service与activity的高强度通信用什么方法? service与activity之前的通信方式有很多,回调接口方式.观察者模式.广播.还有handler等,方法有很多,但要高强度地通信 ...

  4. Android开发学习之路-该怎么学Android(Service和Activity通信为例)

    在大部分地方,比如书本或者学校和培训机构,教学Android的方式都基本类似,就是告诉先上原理方法,然后对着代码讲一下. 但是,这往往不是一个很好的方法,为什么? ① 学生要掌握这个方法的用途,只能通 ...

  5. Android成长日记-Fragment的生命周期与Activity通信

    1. public void onAttach(Activity activity) 当Fragment被添加到Activity时候会回调这个方法,并且这个方法只会被回调一次 2. public vo ...

  6. Activity通信的第三方库——EventBus

    1.可以实现Activity之间高效的通信. 2.较好地实现了监听器和事件之间的解耦. (ps:本人觉得它实际上是一个简易的观察者模式.) 3.用法: //事件接收 public void onEve ...

  7. Android学习笔记(九)一个例子弄清Service与Activity通信

    上一篇博文主要整理了Service的创建.绑定过程,本篇主要整理一下Service与Activity的通信方式.包括在启动一个Service时向它传递数据.怎样改变运行中的Service中得数据和侦听 ...

  8. Android学习——Fragment与Activity通信(二)

    接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) ...

  9. Android学习——Fragment与Activity通信(一)

    学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...

随机推荐

  1. Python文本编辑器推荐

    首推当然是Sublime Text:可以中文化,百度上面有教程,页面比较酷炫,功能也不错 然后就是Notepad++,台湾开发,有中文界面

  2. Python爬虫案例-获取最新的中国行政区域划分

    源网页:中国统计局标准 http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/ 打开网页后可以分析出行政区域划分共分为5层 根据传入参数,生成网页 ...

  3. python多任务抓取图片

    import re import urllib.request import gevent def download(image_download, images_path,i): headers = ...

  4. django+uwsgi+nginx的部署

    1.下载与项目对应的django版本pip3 install django==1.11.16 -i https://pypi.douban.com/simple/2.用django内置的wsgi模块测 ...

  5. 微信支付errcode:40163,code been used,错误小结

    1.配置时注意,支付平台中的支付授权目录, 注意大小写. 昨天碰到的问题,就是自己跳转时,路径写的全小写.跳转支付页面也能跳转过去,但是log中总是调用两次code,报40163错误.后改成和公总号支 ...

  6. webpack 打包报错:One CLI for webpack must be installed. These are recommended choices, delivered as separate packages

    webpack 打包报错: One CLI for webpack must be installed. These are recommended choices, delivered as sep ...

  7. python之地基(一)

    想要建起一座高楼,最重要的就是建一个扎实地基,以下的内容就是地基的一部分,往你用心去阅读,去练习,去掌握. 一.变量 变量是什么?什么是变量?变量有什么好处? 变量是一种使用方便的占位符,用于引用计算 ...

  8. spring 报错

    一. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter 解决方案: 1. ...

  9. vue 结合 FileReader() 实现上传图片预览功能

    项目中 身份证上传需求: <div class="ID_pic_wrap"> <ul> <li> <img src="../.. ...

  10. MVC和MVP设计模式

    参考博客http://www.cnblogs.com/end/archive/2011/06/02/2068512.html ####MVC模式M:model 模型V:view 视图C:control ...