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;
}
}
}

最新文章

  1. Shell编程基础教程2--变量和运算符
  2. HTTP协议之chunk编码(分块传输编码
  3. 转:HTTP 301 跳转和302跳转的区别
  4. P121 6.7 第一题和第二题
  5. [ActionScript 3.0] Away3D 非skybox的全景例子
  6. 《App研发录》知识点汇总
  7. 编写一个递归函数,输出vector对象的内容
  8. Android--图片的三级缓存策略
  9. js监控视频播放的事件并打印log
  10. DFGUI-- 标签交换 Tabstrip
  11. 简单工厂(Simple Factory),最合适的设计模式首秀.
  12. Kubernetes之canal的网络策略(NetworkPolicy)
  13. RTMP、HTTP-FLV、HLS,你了解常见的三大直播协议吗
  14. 从码农到技术总监分享Leader经验
  15. TCP/IP 笔记 - 安全
  16. 一个困扰了我N久的bug , android.enableAapt2=false 无效
  17. 吴裕雄 03-mysql连接
  18. 【BZOJ3551】 [ONTAK2010]Peaks加强版
  19. Hadoop体系结构杂谈
  20. REST Framework 的用户认证组件

热门文章

  1. java 导出
  2. IDEA中 GIT与SVN版本控制插件的切换
  3. 初识springboot(傻瓜式教程)
  4. wireshark 抓包过滤器使用
  5. RNN回归
  6. selenium采用xpath方法识别页面元素
  7. springboo+nginx测试反向代理01
  8. [MySQL]如何支持utf8格式的UCS4unicode字符
  9. 三丶JavaScript 的基础学习(一)
  10. selenium对百度进行登录注销