此文是自己学习bindService的笔记
模拟一个进度条
先贴代码
定义接口, 需要和Activity传递数据
public interface IService {
int getProgress();
}
定义服务
public class MyService extends Service implements IService {
MyBinder myBinder = new MyBinder(); ;
private int progress;
class MyBinder extends Binder implements IService {
@Override
public int getProgress() {
return progress;
}
}
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
@Override
public void onCreate() {
super.onCreate();
new Thread() {
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
updateView(); //更新数据,通知Activity刷新, 此处通过Handler进行通知
}
}
}.start();
}
private void updateView() {
MainActivity.getMyHandler().sendEmptyMessage(1);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, Service.START_REDELIVER_INTENT);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public int getProgress() {
return progress;
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button mOpen, mClose;
static ProgressBar myProgress;
static IService mIservice;
int i = 0;
static MyHandle mmyhandler;
MyService.MyBinder mMyBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOpen = (Button) findViewById(R.id.open);
mOpen.setOnClickListener(this);
mClose = (Button) findViewById(R.id.close);
mClose.setOnClickListener(this);
myProgress = (ProgressBar) findViewById(R.id.pb);
mmyhandler = MyHandle.getInstance();
}
public ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIservice = (IService) service; //通过BindService 使mServiceConnection和MyService有连联系 此处 service==MyBinder 因为mMyBinder实现了IService接口 所以可以强行转换为IService,并且也就获得了IServiced的实现。
// mMyBinder= (MyService.MyBinder) service;
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) { //此方法是Service异常销毁时候才会调用的
Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_SHORT).show();
mServiceConnection = null;
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open:
Intent it = new Intent(this, MyService.class);
bindService(it, mServiceConnection, Service.BIND_AUTO_CREATE);
break;
case R.id.close:
unbindService(mServiceConnection);
break;
default:
break;
}
}
public static MyHandle getMyHandler() { //静态方法,为Service和Activity获取同一个Hanlder
return mmyhandler;
}
public static class MyHandle extends Handler { //使用单例模式
private static MyHandle myHandler;
public static MyHandle getInstance() {
if (myHandler == null) {
myHandler = new MyHandle();
}
return myHandler;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
myProgress.setProgress(mIservice.getProgress());
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.bindservice.MainActivity">
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
style="?android:attr/progressBarStyleHorizontal"
/>
<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close" />
</LinearLayout>
网友评论