startservice與bindservice混合使用問題

來源:互聯網
上載者:User

求人不如 求己 多方查證 沒有找到找到這個問題的答案 於是開啟文檔 照著英文一句一句看 在百度詞霸和我自己的一點英文水平的協助下 終於明白了

實踐證明能看懂文檔是一件多麼重要的事 哎呀 不行要好好學學英語

首先我自己寫了一個service測試程式圖片:

 

圖好像不清楚 湊活看吧 按鈕依次是

start服務

stop服務

bind服務

unbind服務

調用服務的方法

首先我點

start服務

列印資訊:

oncreate()

onstart()

然後我點擊bind沒有列印資訊

然後我點擊stop服務沒有列印資訊                  

然後我點擊unbind

列印出:

unbind 

destory()

好的然後退出activity那麼我們看看服務已經被停止了

好的那麼 我是看的視頻教學裡說的 裡面的老是說 用stop無法銷毀服務 要用unbind 銷毀服務 如果你是先startService再 bindService的話就要這樣做

好的 那麼上面的結果 已經顯示了 老是說的話是正確 但是 你再這樣試一下:

你首先startservice 然後bindservice 然後 unbind 然後你會發現 沒有列印出destory() 方法咦。。。。。。。 奇怪了這是怎麼回事

廢話不多說 總之講師講錯了 我在仔細查閱了文檔之後 發現其實是這樣的:

首先 有一個概念要澄清 那就是一個服務被stop之後不一定會被destory() 但是如果destory()了一定被stop了

這個停止服務 和銷毀服務是兩個概念

那麼來看一下android協助文檔中的原文:

稍微有點 英文基礎的同學應該已經看懂了 這段英文的意思是:

注意如果一個被停止的服務仍然有ServiceConnection對象通過BIND_AUTO_CREATE綁定到它上面,它不會被銷毀知道綁定對象被移除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

有了這個我們理解了 為什麼有這樣現象發生了 它只是stop了服務並沒有銷毀服務 所以 你點stop沒有反應 但是這個時候 服務其實已經被停止了 你再點unbind因為所有綁定到服務上的用戶端都已經解除綁定定了 所以android 作業系統就銷毀了 這個 service

網上有很多人 有誤解 認為 只要綁定到服務上的用戶端全部解除綁定定了 這個服務就被銷毀了 其實也錯了 看下面這段原文:

 和

第一段話的意思是:

如果你同意你的服務被開啟和綁定,然後當服務被開啟的時候,當所有的用戶端都解除對服務的綁定android作業系統也不會銷毀這個服務,相反的你必須顯示的調用stopSelf()

或者stopService()方法來停止服務

第二段話的意思是:

當最後一個用戶端從服務上被解除綁定定的時候作業系統就銷毀服務(除非這個服務是使用startService()的方式啟動的)

從這兩段話就可以看出 問題的所在了

然後 很多人 可能看到了這句話:

這句話 很明顯 也是說 如果是startService 方式開啟才不會被銷毀 因為onstartCommand()很明顯是一個 startService的生命週期中才有的回呼函數

所以說 很多人覺得 他沒有說 startService方式 但是也是 都解除綁定定之後就銷毀了 應該是解除綁定定之後就會銷毀 其實不是這樣的! 它暗含的意思就是告訴你這個方式是startService()

好了 說了 這麼多 總之這個程式的執行流程就是這樣的:

startservice->調用oncreate()方法->stopservice->停止service但沒有銷毀->unbind->調用unbind方法調用destory()方法

結束

好了 寫完了給自己做個筆記 有不對的地方 還請大家指正 謝謝

附上代碼:

package cn.itcast.testService;

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;

public class DemoActivity extends Activity {
 Intent intent ;
 MyConn conn;
 IService iService;
 
 
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  intent = new Intent(this,MyService.class);
  conn = new MyConn();
 }

 public void start_service(View view) {
  startService(intent);
 }

 public void stop_service(View view) {
  stopService(intent);
 }

 public void bind_ervice(View view) {
  bindService(intent, conn, BIND_AUTO_CREATE);
 }

 public void unbind_ervice(View view) {
  unbindService(conn);
 }
 private class MyConn implements ServiceConnection{

  
  public void onServiceConnected(ComponentName name, IBinder service) {
   iService = (IService)service;
   
  }

  
  public void onServiceDisconnected(ComponentName name) {
   // TODO Auto-generated method stub
   
  }
  
 }
 public void call_service(View view){
  iService.callMethodInService();
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();

 }
 
}

 

package cn.itcast.testService;

public interface IService {
 void callMethodInService();
}

 

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {

 @Override
 public IBinder onBind(Intent intent) {
  
  
  return new MyBinder();
 }
 
 
 public class MyBinder extends Binder implements IService{

  
  public void callMethodInService() {
  
   helloInservice();
  }
  
 }
 public void helloInservice(){
  System.out.println("hello in service");
 }

 @Override
 public void onCreate() {
  System.out.println("create");
  super.onCreate();
 }

 @Override
 public void onStart(Intent intent, int startId) {
  // TODO Auto-generated method stub
  super.onStart(intent, startId);
  System.out.println("onStart");
 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  System.out.println("destroy");
 }

 @Override
 public boolean onUnbind(Intent intent) {
  // TODO Auto-generated method stub
  System.out.println("onUnbind");
  return super.onUnbind(intent);

 }

 
 
 
}

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.