创建一个MyService在里面写入
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
/**
*
* 通过这个方法,可以在 activity 中拿到 MyService 对象,就可以调用他的方法了
*
*/
class MyBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
在 MainActivity 中 connection 拿到 MyService 对象
/**
* 把 service 链接起来
* 拿到 service 对象
*/
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//返回一个MsgService对象
myService = ((MyService.MyBinder) service).getService();
}
};
在 MyActivity 中绑定 Service
// 绑定 service
Intent bindIntent = new Intent(context, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
上面3步实现了,activity 调用 service中的方法
比如调用 myService.startDownLoad(); 模拟下载文件
那么下载文件的进度 service 怎样通知activity呢
可以在 MyService 中写回调方法,在 MainActivty 中调用
如:MyService 中
/**
* 写一个接口回调,给 activity 拿到返回的值
*/
private MyProgressListener myProgressListener;
public void setOnProgressListener(MyProgressListener myProgressListener) {
this.myProgressListener = myProgressListener;
}
public interface MyProgressListener {
void onProgress(int progress);
}
MainActivty中
//响应接口返回的数据
myService.setOnProgressListener(new MyService.MyProgressListener() {
@Override
public void onProgress(int progress) {
textView.setText("显示进度"+progress);
}
});
完整代码如下(复制即可运行)==========================
MyService
package com.example.zhhservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
/**
* 写一个接口回调,给 activity 拿到返回的值
*/
private MyProgressListener myProgressListener;
public void setOnProgressListener(MyProgressListener myProgressListener) {
this.myProgressListener = myProgressListener;
}
public interface MyProgressListener {
void onProgress(int progress);
}
/**
* 模拟下载任务
*/
int i = 0;
public void startDownLoad(){
new Thread(new Runnable() {
@Override
public void run() {
// 死循环,看数据
while (true){
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
i++;
myProgressListener.onProgress(i);
Log.e("111",""+i);
}
}
}).start();
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
/**
*
* 通过这个方法,可以在 activity 中拿到 service 对象
*
*/
class MyBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
}
MainActivity
package com.example.zhhservice;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// 上下文
private Context context;
// 模拟下载的按钮
private Button button1;
// 显示进度的控件
private TextView textView;
// 绑定的service的对象
private MyService myService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initCliock();
}
/**
* 把 service 链接起来
* 拿到 service 对象
*/
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//返回一个MsgService对象
myService = ((MyService.MyBinder) service).getService();
//响应接口返回的数据
myService.setOnProgressListener(new MyService.MyProgressListener() {
@Override
public void onProgress(int progress) {
textView.setText("显示进度"+progress);
}
});
}
};
/**
* 初始化控件
*/
private void initView(){
context = MainActivity.this;
// 绑定 service
Intent bindIntent = new Intent(context, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
// 点击按钮
button1 = findViewById(R.id.button1);
// 显示进度
textView = findViewById(R.id.textView);
}
/**
* 点击事件
* 模拟service的下载任务
*/
private void initCliock(){
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myService.startDownLoad();
}
});
}
/**
*
* 取消绑定 service
*
*/
@Override
protected void onDestroy() {
unbindService(connection);
super.onDestroy();
}
}//class
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模拟下载"
/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
参考文章:
https://blog.youkuaiyun.com/xiaanming/article/details/9750689