此文是自己學(xué)習(xí)bindService的筆記
模擬一個(gè)進(jìn)度條
先貼代碼
定義接口搏恤, 需要和Activity傳遞數(shù)據(jù)
public interface IService {
int getProgress();
}
定義服務(wù)
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(); //更新數(shù)據(jù)毁嗦,通知Activity刷新宫补, 此處通過Handler進(jìn)行通知
}
}
}.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有連聯(lián)系 此處 service==MyBinder 因?yàn)閙MyBinder實(shí)現(xiàn)了IService接口 所以可以強(qiáng)行轉(zhuǎn)換為IService尸曼,并且也就獲得了IServiced的實(shí)現(xiàn)。
// mMyBinder= (MyService.MyBinder) service;
Toast.makeText(MainActivity.this, "連接成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) { //此方法是Service異常銷毀時(shí)候才會(huì)調(diào)用的
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() { //靜態(tài)方法,為Service和Activity獲取同一個(gè)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>