Android—Service與Activity的互動,androidactivity
service—Android的四大組件之一。人稱“後台服務”指其本身的運行並不依賴於使用者可視的UI介面
實際開發中我們經常需要service和activity之間可以相互傳遞資料維持程式的運行。
先瞭解Service的生命週期吧。
建立一個類繼Service:
package com.example.myservicedemo.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;/** * 服務類(需要在項目資訊清單檔中註冊服務) * * @author lenovo * */public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;} /** * 服務建立的時候調用 */@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("=========onCreate======");} /** * 服務啟動的時候調用 */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("=========onStartCommand======");return super.onStartCommand(intent, flags, startId);} /** * 服務銷毀的時候調用 */@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("=========onDestroy======");super.onDestroy();}}
建立以上類並繼承Service後只會重寫onBind()方法,其他方法是我手動手寫,為了弄清楚Service的生命週期
MainActivity中(設定兩個按鈕用來開始和停止服務):
package com.example.myservicedemo.ui;import com.example.myservicedemo.R;import com.example.myservicedemo.service.MyService;import com.example.myservicedemo.service.MyService.DownLoadBinder;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.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_start = (Button) findViewById(R.id.btn_start);Button btn_stop = (Button) findViewById(R.id.btn_stop);btn_start.setOnClickListener(this);btn_stop.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();switch (id) {/* * 開啟服務點擊事件 */case R.id.btn_start:Intent startIntent = new Intent(this, MyService.class);startService(startIntent);break;/* * 停止服務點擊事件 */case R.id.btn_stop:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent);break;default:break;}}}
切記android中的服務是需要在項目資訊清單檔中註冊的(AndroidStudio可以自動,eclipse需要手動添加):
<service android:name="服務類坐所在的包名.MyService"></service>
此時運行程式,點擊開啟服務時候輸出是:(我輸出多次onStartCommand()是因為我連續點擊了多次開啟服務按鈕)
可以看出服務第一次開啟時先是調用了onCreate()方法和onStartCommand()方法,多次點擊開始服務時只調用了onStartCommand()方法
so:
onCreate()方法是服務建立的時候調用的~
onStartCommand()方法在每次啟動服務的時候都會調用~
onDestory()方法在停止服務時候會調用~
點擊停止服務後,輸出
啟動服務還有一種方式是bindService();
此時的MyService類要做改變:
package com.example.myservicedemo.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;/** * 服務類(需要在項目資訊清單檔中註冊服務) * * @author lenovo * */public class MyService extends Service { private DownLoadBinder downLoadBinder=new DownLoadBinder();@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("=====onBind=====");return downLoadBinder;}/** * 內部類繼承Binder * @author lenovo * */public class DownLoadBinder extends Binder{public void startDownLoad(){System.out.println("=====startDownLoad()=====");}public void getProgress(){System.out.println("=====getProgress()=====");}} /** * 服務建立的時候調用 */@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("=========onCreate======");} /** * 服務啟動的時候調用 */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("=========onStartCommand======");return super.onStartCommand(intent, flags, startId);} /** * 服務銷毀的時候調用 */@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("=========onDestroy======");super.onDestroy();}}
以上的代碼和第一次相比,一是多了一個內部類DownLoadBinder繼承IBinder並且聲明了兩個方法,二是將onBind方法的傳回值改為了DownLoadBinder類型的變數
activity bindService方法啟動服務時候一般是需要傳遞資料的,核心就在onBind()方法中,往下看
MainActivity中加兩個按鈕:綁定服務和取消綁定服務
package com.example.myservicedemo.ui;import com.example.myservicedemo.R;import com.example.myservicedemo.service.MyService;import com.example.myservicedemo.service.MyService.DownLoadBinder;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.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener { private MyService.DownLoadBinder downLoadBinder; private MyService myService; //我們自己的service @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_start = (Button) findViewById(R.id.btn_start);Button btn_stop = (Button) findViewById(R.id.btn_stop);Button btn_bind = (Button) findViewById(R.id.btn_bind);Button btn_unbind = (Button) findViewById(R.id.btn_unbind);btn_start.setOnClickListener(this);btn_stop.setOnClickListener(this);btn_bind.setOnClickListener(this);btn_unbind.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();switch (id) {/* * 開啟服務點擊事件 */case R.id.btn_start:Intent startIntent = new Intent(this, MyService.class);startService(startIntent);break;/* * 停止服務點擊事件 */case R.id.btn_stop:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent);break;/* * 綁定服務點擊事件 */case R.id.btn_bind:Intent bindIntent = new Intent(this, MyService.class);bindService(bindIntent, connection, BIND_AUTO_CREATE);break;/* * 解除綁定服務點擊事件 */case R.id.btn_unbind: unbindService(connection);break;default:break;}}private ServiceConnection connection=new ServiceConnection() {/** * 服務解除綁定時候調用 */@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}/** * 綁定服務的時候調用 */@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub//myService=((DownLoadBinder) service).downLoadBinder=(DownLoadBinder) service;/* * 調用DownLoadBinder的方法實現參數的傳遞 */downLoadBinder.startDownLoad();downLoadBinder.getProgress();}};}
運行後點擊綁定服務後輸出如下:
說明成功綁定了服務且傳遞了資料,點擊解除綁定服務時候ondestory()方法輸出
不過這種方法好像只能傳遞一次資料,,,,不爽,,,,比如後台在即時更新東西,activity需要即時擷取呢???
尋找資料大多是以下幾種方式:
1.使用介面回調方式,activity實現相應的介面,service通過介面進行回調,比較靈活
2.使用廣播
這篇部落客要介紹第一種方法,為什麼不介紹第二種???——不願意介紹,不喜歡。
使用介面回調方式的MyService和MainActivity代碼我是備忘詳細之後才粘貼的,大家可以看注釋
MyService代碼:
package com.example.myservicedemo.service;import java.util.Timer;import java.util.TimerTask;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;/** * 服務類(需要在項目資訊清單檔中註冊服務) * * @author lenovo * */public class MyService extends Service { private DownLoadBinder downLoadBinder=new DownLoadBinder(); /** * 回調 */private Callback callback;/** * Timer即時更新資料的 */private Timer mTimer=new Timer();/** * */private int num;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("=====onBind=====");return downLoadBinder;}/** * 內部類繼承Binder * @author lenovo * */public class DownLoadBinder extends Binder{/** * 聲明方法傳回值是MyService本身 * @return */public MyService getService() {return MyService.this;}} /** * 服務建立的時候調用 */@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();/* * 執行Timer 2000毫秒後執行,5000毫秒執行一次 */mTimer.schedule(task, 0, 1000);}/** * 提供介面回調方法 * @param callback */public void setCallback(Callback callback) {this.callback = callback;}/** * */TimerTask task = new TimerTask(){@Overridepublic void run() {// TODO Auto-generated method stubnum++;if(callback!=null){/* * 得到最新資料 */callback.getNum(num);}}};/** * 回調介面 * * @author lenovo * */public static interface Callback {/** * 得到即時更新的資料 * * @return */void getNum(int num);} /** * 服務銷毀的時候調用 */@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("=========onDestroy======");/** * 停止Timer */mTimer.cancel();super.onDestroy();}}
MainActivity代碼:
package com.example.myservicedemo.ui;import com.example.myservicedemo.R;import com.example.myservicedemo.service.MyService;import com.example.myservicedemo.service.MyService.Callback;import com.example.myservicedemo.service.MyService.DownLoadBinder;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.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener { private MyService.DownLoadBinder downLoadBinder; // private MyService myService; //我們自己的service @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_start = (Button) findViewById(R.id.btn_start);Button btn_stop = (Button) findViewById(R.id.btn_stop);Button btn_bind = (Button) findViewById(R.id.btn_bind);Button btn_unbind = (Button) findViewById(R.id.btn_unbind);btn_start.setOnClickListener(this);btn_stop.setOnClickListener(this);btn_bind.setOnClickListener(this);btn_unbind.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();switch (id) {/* * 開啟服務點擊事件 */case R.id.btn_start:Intent startIntent = new Intent(this, MyService.class);startService(startIntent);break;/* * 停止服務點擊事件 */case R.id.btn_stop:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent);break;/* * 綁定服務點擊事件 */case R.id.btn_bind:Intent bindIntent = new Intent(this, MyService.class);bindService(bindIntent, connection, BIND_AUTO_CREATE);break;/* * 解除綁定服務點擊事件 */case R.id.btn_unbind: unbindService(connection);break;default:break;}}private ServiceConnection connection=new ServiceConnection() {/** * 服務解除綁定時候調用 */@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}/** * 綁定服務的時候調用 */@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubdownLoadBinder = (DownLoadBinder) service;MyService service2 = downLoadBinder.getService();/** * 實現回調,得到即時重新整理的資料 */service2.setCallback(new Callback() {@Overridepublic void getNum(int num) {// TODO Auto-generated method stubSystem.out.println("====num===="+num);}});}};}
運行後的結果應該輸出就對了(輸出隨service中num的數量而更新):
期間遇到的問題:
bindService後無效,MyService類中的onBind()方法沒有被調用
解決:這種情況下你的Activity應該是繼承了TabBarActivity,綁定服務的時候調用寫這種形式:
this.getApplicationContext().bindService(intent, mConnection, BIND_AUTO_CREATE);
最近幾天用到,總結一下,希望可以為大家帶來協助,有錯誤還請大神指正。