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