活動和服務進行通訊
阿新 • • 發佈:2018-11-25
我們學習了啟動和停止服務的方法,雖然服務是在活動裡啟動的,但在啟動了服務之後,活動與服務基本上就沒有什麼關係了。確實如此,我們在活動裡呼叫了StartCommand()方法來啟動MyService這個服務,然後MyService的onCreate()和onStartCommand()方法就會得到執行。之後服務就一直處於執行狀態,但具體執行的是什麼邏輯,活動就控制不了了。這就類似於活動通知了服務一下:“你可以啟動了!”然後服務就去忙自己的事情了,但活動並不知道服務到底去做了什麼事情,以及完成的如何。
比如說,目前我們希望在MyService裡提供一個下載功能,然後在活動中可以決定何時開始下載,以及隨時檢視下載進度。實現這個功能的思路是建立一個專門的Binder物件來對下載功能進行管理,修改MyService中的程式碼
package com.example.yxp.service; 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 { private DownloadBinder mBinder = new DownloadBinder(); class DownloadBinder extends Binder { public void startDownload(){ Log.d("MyService","startDownload executed"); } public int getProgress(){ Log.d("MyService","getProgress executed"); return 0; } } public IBinder onBind(Intent intent){ return mBinder; } public MyService() { } public void onCreate(){ super.onCreate(); Log.d("MyService","onCreate executed"); } public int onStartCommand(Intent intent,int flags,int startId){ Log.d("MyService","onStartCommand executed"); return super.onStartCommand(intent,flags,startId); } public void onDestroy(){ super.onDestroy(); Log.d("MyService", "onDestroy executed"); } }
修改佈局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service"/> <Button android:id="@+id/stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service"/> <Button android:id="@+id/bind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bind Service"/> <Button android:id="@+id/unbind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Unbind Service"/> </LinearLayout>
修改MainActivity檔案內容
package com.example.yxp.service;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.sql.BatchUpdateException;
public class MainActivity extends Activity {
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startService = (Button) findViewById(R.id.start_service);
Button stopService = (Button) findViewById(R.id.stop_service);
startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startIntent = new Intent(MainActivity.this,MyService.class);
startService(startIntent);
}
});
stopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent stopIntent = new Intent(MainActivity.this,MyService.class);
stopService(stopIntent);
}
});
final Button bindService = (Button) findViewById(R.id.bind_service);
final Button unbindService = (Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent bindIntent = new Intent(MainActivity.this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
}
});
unbindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unbindService(connection);
}
});
}
}