1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android---Service(生命周期 启动方式 服务通信 实战演练 思维导图 高级音乐播放器-源码)

Android---Service(生命周期 启动方式 服务通信 实战演练 思维导图 高级音乐播放器-源码)

时间:2021-02-17 06:53:12

相关推荐

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="/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 上。

/lwx001/Service

筛选LogCat信息 :

运行效果图:

(2)bindService 方式 启动 服务

实战演练---bindService

源码【工程文件】--- 可以直接用Gitee拷贝到Android Studio 上。

/lwx001/Service2

MainActivity.java :

package cn.lwx.service;import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.widget.DialogTitle;import ponentName;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="/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>

运行效果图 :

四、服务的通信

实战演练---音乐播放器

/1vVLYnlb

/lwx001/MusicPlayer

运行程序,没有声音。不知道为啥,QAQ ~ Help~

Service(服务)思维导图

音乐播放器---高级实战(源码)

源码【工程文件】:可直接用gitee拷贝至个人的Android Studio上。

【文件丢失:可联系QQ:386335886】

/wang_zhiguo/Course0902

添加音乐-详细步骤

运行截图

菜鸟教程【Service初涉】

菜鸟教程【Service初涉】

/w3cnote/android-tutorial-service-1.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。