https://blog.youkuaiyun.com/xiaanming/article/details/9750689#commentBox
这个讲的太好了直接看这个吧
新建一个msgService类继承Serivice
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MsgService extends Service {
//进度条的最大值
public static final int MAX_PROGRESS = 100;
//进度条的进度值
private int progress = 0;
/**
* 增加get()方法,供Activity调用
*
* @return 下载进度
*/
public int getProgress() {
return progress;
}
/**
* 模拟下载任务,每秒钟更新一次
*/
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();
}
/**
* 返回一个Binder对象
*/
@Override
public IBinder onBind(Intent intent) {
return new MsgBinder();
}
public class MsgBinder extends Binder {
/**
* 获取当前Service的实例
*
* @return
*/
public MsgService getService() {
return MsgService.this;
}
}
}
Activity
import androidx.appcompat.app.AppCompatActivity;
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.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MsgService msgService;
private int progress = 0;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定service
Intent intent = new Intent(this, MsgService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
mProgressBar = findViewById(R.id.progressBar1);
Button mButton = findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开始下载
msgService.startDownLoad();
//监听进度
listenProgress();
}
});
}
//造ServiceConnection 对象 重写内部方法
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//返回一个MsgService对象
Log.i("godv", "onServiceConnected" + service);
msgService = ((MsgService.MsgBinder) service).getService();
}
};
/**
* 监听进度,每秒钟获取调用MsgService的getProgress()方法来获取进度,更新UI
*/
public void listenProgress() {
new Thread(new Runnable() {
@Override
public void run() {
while (progress < MsgService.MAX_PROGRESS) {
// progress = msgService.getProgress();
// mProgressBar.setProgress(progress);
try {
Thread.sleep(1000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "godv", Toast.LENGTH_SHORT).show();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
注册
<service android:name="com.example.service_count.MsgService" />