日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法

發布時間:2023/12/20 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.綁定服務調用服務里面的方法,圖解:

步驟:

(1)在Activity代碼里面綁定 bindService(),以bind的方式開啟服務?

? ? ? ? ? ? ? ? ? ??bindServiceintent, new MyConn(), BIND_AUTO_CREATE);

參數intent:意圖對象,服務對應的意圖對象 ?new ?Intent(this,Service.class)

參數ServiceConnection (接口,自定義其接口實現內部類MyConn() ):通訊頻道,利用他可以獲取服務成功綁定后得到的秘書

參數BIND_AUTO_CREATE:常量,服務不存在會自動創建

?

(2)實現MyConn接口實現內部類

?

/*** 服務連接成功的通訊頻道**/private class MyConn implements ServiceConnection{//當服務被成功連接的時候調用的方法 @Overridepublic void onServiceConnected(ComponentName name, IBinder service) {重要參數IBinder,代表的就是中間人,服務的秘書 }//當服務失去連接的時候調用的方法 @Overridepublic void onServiceDisconnected(ComponentName name) { } }

?

?

(3)如果服務被成功綁定 ?會執行onBind的方法? ? ? ?

public IBinder onBind (Intent intent )

這個方法的返回值為 IBinder 就是服務內部的秘書

?

(4)擴展實現服務內部的秘書,可以間接的調用服務的方法

/*** 服務內部的秘書,可以調用服務的方法**/public class MyBinder extends Binder{/*** 調用服務的方法。* @param money 錢*/public void callMethodInService(int money){if(money>500){methodInService();}else{Toast.makeText(DemoService.this, "這點錢還想辦事呀?", 0).show();}}}

?

(5)在MyConn成功綁定的時候,就得到了IBInder對象, MyBinder

(6)利用MyBinder間接調用服務的方法

?

2.案例代碼:

(1)布局文件activity_main.xml

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <Button 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:onClick="bind" 12 android:text="綁定服務" /> 13 14 <Button 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:onClick="unbind" 18 android:text="解除綁定服務" /> 19 20 <Button 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:onClick="call" 24 android:text="調用服務里面的方法" /> 25 26 </LinearLayout>

布局效果如下:

(2)MainActivity.java

1 package com.itheima.bind; 2 3 import com.itheima.bind.DemoService.MyBinder; 4 5 import android.app.Activity; 6 import android.content.ComponentName; 7 import android.content.Intent; 8 import android.content.ServiceConnection; 9 import android.os.Bundle; 10 import android.os.IBinder; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 MyBinder myBinder; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 } 21 22 protected void onDestory(Bundle savedInstanceState) { 23 super.onDestory(); 24 ----;//解綁服務 25 } 26 27 /** 28 * 綁定服務,獲取服務里面的秘書,間接的調用服務里面的方法。 29 * @param view 30 */ 31 public void bind(View view){ 32 Intent intent = new Intent(this,DemoService.class); 33 //intent 意圖 34 //conn 服務的通訊頻道 35 //1 服務如果在綁定的時候不存在,會自動創建 36 System.out.println("1.采用bind的方式開啟服務"); 37 bindService(intent, new MyConn(), BIND_AUTO_CREATE); 38 } 39 40 41 /** 42 * 解綁服務 43 * @param view 44 */ 45 public void unbind(View view){ 46 47 System.out.println("解綁服務"); 48 if(myBinder != null) { 49 unbindService(new MyConn()); 50 myBinder = null; 51 } 52 53 } 54 /** 55 * 服務連接成功的通訊頻道 56 * 57 */ 58 private class MyConn implements ServiceConnection{ 59 //當服務被成功連接的時候調用的方法 60 @Override 61 public void onServiceConnected(ComponentName name, IBinder service) { 62 System.out.println("3. 得到了服務的一個連接,通訊頻道,獲取到服務內部的秘書"); 63 myBinder = (MyBinder) service; 64 System.out.println("4.把ibinder強制類型轉化成秘書MyBinder"); 65 } 66 //當服務失去連接的時候調用的方法 67 @Override 68 public void onServiceDisconnected(ComponentName name) { 69 70 } 71 } 72 73 /** 74 * 調用服務里面的方法。 75 * @param view 76 */ 77 public void call(View view){ 78 System.out.println("5.利用mybinder間接的調用服務的方法"); 79 myBinder.callMethodInService(3000); 80 } 81 }

?

其中創建的服務DemoService.java如下:

1 package com.itheima.bind; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.widget.Toast; 8 9 public class DemoService extends Service { 10 11 /** 12 * 在服務被綁定的時候調用的方法 13 * 14 * IBinder 服務內部的秘書 15 */ 16 @Override 17 public IBinder onBind(Intent intent) { 18 System.out.println("2. 服務如果成功綁定會執行onbind,返回服務內部的秘書 mybinder"); 19 return new MyBinder(); 20 } 21 /** 22 * 服務內部的秘書,可以調用服務的方法 23 * 24 */ 25 public class MyBinder extends Binder{ 26 /** 27 * 調用服務的方法。 28 * @param money 錢 29 */ 30 public void callMethodInService(int money){ 31 if(money>500){ 32 methodInService(); 33 }else{ 34 Toast.makeText(DemoService.this, "這點錢還想辦事呀?", 0).show(); 35 } 36 } 37 } 38 39 40 41 /** 42 * 服務里面的方法 43 */ 44 public void methodInService(){ 45 Toast.makeText(this, "哈哈,我是服務的方法,被你調用了。", 0).show(); 46 } 47 48 @Override 49 public void onCreate() { 50 System.out.println("服務被創建了"); 51 super.onCreate(); 52 } 53 @Override 54 public void onDestroy() { 55 System.out.println("服務被銷毀了。"); 56 super.onDestroy(); 57 } 58 }

這里定義一個Service,當然要在AndroidMainfest.xml文件中注冊一下:

1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.itheima.bind" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="17" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.itheima.bind.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <service android:name="com.itheima.bind.DemoService"></service> 26 </application> 27 28 </manifest>

?

3.?綁定服務的應用場景

?提供一個服務,后臺運行,里面有一些公共的邏輯供調用.

>1. 微信支付, 微信有一個支付的服務,綁定,調用支付的方法
>2. sony手機,人臉識別的服務,綁定到這個服務傳遞一個照片就會把人臉標記出來
>3. 音樂播放器,后臺服務里面播放音樂綁定服務暫停下一曲上一曲

轉載于:https://www.cnblogs.com/hebao0514/p/4800175.html

總結

以上是生活随笔為你收集整理的Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 色婷婷av一区二区三区大白胸 | 精品国产一 | 欧美在线观看视频一区二区 | 欧洲三级视频 | 亚洲一区在线视频 | 亚洲欧洲日韩综合 | 日韩 欧美 亚洲 国产 | 午夜免费福利小电影 | 色婷婷综合久久久中文字幕 | 亚洲无码精品在线播放 | 91黄免费| 在线中文字幕亚洲 | 亚州精品国产精品乱码不99按摩 | 亚洲欧美婷婷 | 日韩一级片免费在线观看 | 一区二区三区精品在线观看 | 四虎永久在线 | v99av| 一本黄色片 | av中字| 污污内射久久一区二区欧美日韩 | 久草影视网| 91禁国产网站 | 与子敌伦刺激对白播放的优点 | 亚洲免费一级 | 噜噜噜视频 | 久久成人乱码欧美精品一区二区 | 91sao| 亚洲男人网 | 国产婷婷一区二区三区久久 | 香蕉国产在线观看 | 国内视频一区二区三区 | 97se视频| 男女猛烈无遮挡免费视频 | 精品美女一区二区三区 | 青青青视频免费观看 | 久久国产激情视频 | 欧美巨大另类极品videosbest | 色综合av综合无码综合网站 | www.国产.com | 伊人手机视频 | 久久免费看视频 | 一本一道波多野结衣一区二区 | 干干干日日日 | 成人在线免费播放视频 | 日本在线视频www | 国产精品污网站 | 制服 丝袜 综合 日韩 欧美 | 欧美三级一区二区 | 国产a不卡| 精品美女在线观看 | 丰满人妻av一区二区三区 | 日本加勒比中文字幕 | 手机看片国产精品 | 国内自拍在线 | 国产91色 | 国产精品7777 | 99热热99 | 最新欧美日韩 | 诱夫1v1高h| 医生强烈淫药h调教小说视频 | 中文字幕亚洲在线观看 | 九九色综合网 | 熊猫av| 亚洲精品在线免费 | 午夜激情婷婷 | 双性娇喘浑圆奶水h男男漫画 | 在线超碰av | 国产三级av在线播放 | 亚洲小视频在线播放 | 一区二区啪啪啪 | 天天天操 | 亚洲综合免费观看高清完整版 | 国产综合无码一区二区色蜜蜜 | 日韩免费淫片 | 久久天天东北熟女毛茸茸 | h网站在线| 久久av免费看 | 一区二区三区精品久久久 | 幸福,触手可及 | 狠狠撸在线观看 | va婷婷在线免费观看 | 亚洲日b视频 | 日本韩国中文字幕 | 2021久久 | 狠狠躁狠狠躁视频专区 | 欧美日韩激情在线 | 懂色av懂色av粉嫩av分享吧 | 男人的天堂黄色 | 欧美嫩草 | 亚洲精品91在线 | 黄页网站视频在线观看 | 色骚网| 在线观看黄色免费网站 | 久久久精品91 | 日本三级中文字幕 | 精产国产伦理一二三区 | 亚洲一区二区三区影视 | 91久久伊人 |