Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)
目? ?錄
一、服務的創建
二、服務的生命周期
三、服務的啟動方式
(1)startService 方式 啟動 服務
實戰演練---startService
(2)bindService 方式 啟動 服務
實戰演練---bindService
四、服務的通信
實戰演練---音樂播放器
Service(服務)思維導圖
音樂播放器---高級實戰(源碼)
添加音樂-詳細步驟
運行截圖
?菜鳥教程【Service初涉】
一、服務的創建
?
二、服務的生命周期
三、服務的啟動方式
(1)startService 方式 啟動 服務
服務的啟動方式 :
startService()方法啟動服務,服務會長期的在后臺運行,并且服務的狀態與開啟者的狀態沒有關系,
即使啟動服務的組件已經被銷毀,服務會依舊運行。
實戰演練---startService
relative.xml :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/btn_stop"android:layout_centerHorizontal="true"android:layout_marginBottom="90dp"android:background="#B0E0E6"android:onClick="start"android:text="開啟服務"android:textColor="#6C6C6C"android:textSize="18sp"/><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/btn_start"android:layout_alignParentBottom="true"android:layout_marginBottom="25dp"android:background="#080808"android:onClick="stop"android:text="關閉服務"android:textColor="#6C6C6C"android:textSize="18sp"/> </RelativeLayout>MyService.java :
package cn.lwx.service;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log;public class MyService extends Service {public MyService() {}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service. // throw new UnsupportedOperationException("Not yet implemented");return null;}@Overridepublic void onCreate() {super.onCreate();Log.i("StartService","onCreate()");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i("StartService","onStartCommand()");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.i("StartService","onDestroy()");} }MainActivity.java :
package cn.lwx.service;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.os.Bundle; import android.view.View;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.relative);}// 開啟服務的方法public void start(View view){Intent intent = new Intent(this, MyService.class);startService(intent);}// 關閉服務的方法public void stop(View view){Intent intent = new Intent(this, MyService.class);stopService(intent);} }源碼【工程文件】--- 可以直接用Gitee拷貝到Android Studio 上。
https://gitee.com/lwx001/Service
篩選LogCat信息 :
運行效果圖?:?
?
(2)bindService 方式 啟動 服務
實戰演練---bindService
源碼【工程文件】--- 可以直接用Gitee拷貝到Android Studio 上。
https://gitee.com/lwx001/Service2
MainActivity.java :
package cn.lwx.service;import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.DialogTitle;import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View;public class MainActivity<btnUnbind> extends AppCompatActivity {private MyService2.MyBinder myBinder;//成員變量保存返回值private MyConn myconn; //內部類@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.relative2);}// // ---Service1--- // // 開啟服務的方法 // public void start(View view){ // Intent intent = new Intent(this, MyService.class); // startService(intent); // } // // // 關閉服務的方法 // public void stop(View view){ // Intent intent = new Intent(this, MyService.class); // stopService(intent); // }//綁定服務public void btnBind(View view) {//避免重復創建myconnif (myconn == null) {myconn = new MyConn();}Intent intent = new Intent(this, MyService2.class);//參數1是Intent,參數2是連接對象,參數3是flags-表示:如果服務不存在就創建服務bindService(intent, myconn, BIND_AUTO_CREATE);}//解綁服務public void btnUnbind(View view) {if (myconn != null) {unbindService(myconn);myconn = null;}}//調用服務中的方法public void btnCall(View view) {myBinder.callMethodInService();}//創建內部類MyConn,用于實現連接服務private class MyConn implements ServiceConnection {//當成功綁定到服務時,調用的方法,返回MyService2里面的Ibinder對象@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {myBinder = (MyService2.MyBinder) service;Log.i("MainActivity", "服務綁定成功,內存地址為:" + myBinder.toString());}//當服務失去連接時,調用的方法@Overridepublic void onServiceDisconnected(ComponentName name) {}}}MyService2.java :
package cn.lwx.service;import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log;public class MyService2 extends Service {private static final String TAG = "MyService2"; // logt 回車public MyService2() {}//創建服務的代理,調用服務中的方法class MyBinder extends Binder {public void callMethodInService() {methodInService();}}@Overridepublic void onCreate() {Log.i("MyService2", "創建服務,調用onCreate()方法");super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service. // throw new UnsupportedOperationException("Not yet implemented");Log.i("MyService2", "綁定服務,調用onBind()");return new MyBinder();}public void methodInService() {Log.i("MyService2", "自定義方法 methodInService()");}@Overridepublic boolean onUnbind(Intent intent) {Log.i("MyService2", "解綁服務,調用onUnbind()");return super.onUnbind(intent);} }relative.xml :?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg1"android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_call"android:layout_alignLeft="@+id/btn_call"android:layout_marginBottom="50dp"android:background="#F5F5DC"android:onClick="btnBind"android:text="綁定服務"android:textSize="16sp" /><Buttonandroid:id="@+id/btn_call"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_unbind"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_marginEnd="30dp"android:layout_marginRight="30dp"android:layout_marginBottom="55dp"android:background="#F5F5DC"android:onClick="btnCall"android:paddingLeft="5dp"android:paddingRight="5dp"android:text="調用服務中的方法"android:textSize="16sp" /><Buttonandroid:id="@+id/btn_unbind"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/btn_call"android:layout_alignParentBottom="true"android:layout_marginBottom="70dp"android:background="#F5F5DC"android:onClick="btnUnbind"android:text="解綁服務"android:textSize="16sp" /> </RelativeLayout>運行效果圖 :
四、服務的通信
實戰演練---音樂播放器
https://share.weiyun.com/1vVLYnlb
https://gitee.com/lwx001/MusicPlayer
運行程序,沒有聲音。不知道為啥,QAQ ~? ?Help~
Service(服務)思維導圖
音樂播放器---高級實戰(源碼)
源碼【工程文件】:可直接用gitee拷貝至個人的Android Studio上。
【文件丟失:可聯系QQ:386335886】
https://gitee.com/wang_zhiguo/Course0902
添加音樂-詳細步驟
運行截圖
?菜鳥教程【Service初涉】
菜鳥教程【Service初涉】
https://www.runoob.com/w3cnote/android-tutorial-service-1.html
總結
以上是生活随笔為你收集整理的Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Activity (项目实战:选择宝宝装
- 下一篇: Android-广播接收者简介