2.四大组件之Service
1.理论概述
2.1本地service
2.2service的生命周期
3.远程service
3.1理解
3.2使用远程service
4.应用联系
4.1使用本地service播放音乐
4.2使用远程service挂断电话
4.3实现黑名单电话的自动拦截
service是什么?
service是一个应用组件,在后台完成一个时间跨度较大的工作且没有关联任何界面。
一个service可以完成下面这些工作:
访问网络(service要在主线程执行,访问网络在分线程执行,那咋解救这个冲突?在service运行分线程)
播放音乐
文件IO操作
大数据量的数据库操作
服务的特点:Service在后台运行,不会与用户进行交互
即使应用退出,服务也不会停止
启动服务,停止服务,绑定服务,解绑服务 以及在绑定服务的这一大类中能够使用service的函数的功能
前端
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/startServiceBtn" android:text="启动服务" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MyStartService"/> <Button android:id="@+id/closeServiceBtn" android:text="关闭服务" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MyCloseService" /> <Button android:id="@+id/bindServiceBtn" android:text="绑定服务" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MyBindService"/> <Button android:id="@+id/unBindServiceBtn" android:text="解绑服务" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MyUnBindService" /> </LinearLayout>
MainActivity.class
package com.example.myservice; import androidx.appcompat.app.AppCompatActivity; 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; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //开启服务 public void MyStartService(View view) { Intent intent = new Intent(this,MyService.class); startService(intent); Toast.makeText(this,"服务已开启",Toast.LENGTH_SHORT).show(); } //关闭服务 public void MyCloseService(View view) { Intent intent = new Intent(this,MyService.class); stopService(intent); Toast.makeText(this,"服务已开启",Toast.LENGTH_SHORT).show(); } //************************************连接对象************************************** ServiceConnection conn = new ServiceConnection() { //与服务连接上的时候调用这个方法 @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.e("TAG","onServiceConnected************"); //在连接中试着调用service中的方法 MyService.yyBinder hello = (MyService.yyBinder) iBinder; hello.getName(); } //与服务断开连接上的时候调用这个方法 @Override public void onServiceDisconnected(ComponentName componentName) { Log.e("TAG","onServiceDisconnected************"); } }; //**********************************绑定服务************************************************** public void MyBindService(View view) { Intent intent = new Intent(this,MyService.class); //绑定服务 bindService(intent,conn,BIND_AUTO_CREATE); Toast.makeText(this,"绑定服务(另类的开启服务)",Toast.LENGTH_SHORT).show(); } //*********************************解绑服务************************************************* public void MyUnBindService(View view) { Intent intent = new Intent(this,MyService.class); //解绑服务 unbindService(conn); Toast.makeText(this,"解绑服务",Toast.LENGTH_SHORT).show(); } }
Myservice.class
package com.example.myservice; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Toast; import java.util.concurrent.Executor; /**************************************自定义本地服务***************************************/ public class MyService extends Service { public MyService() { } //开启服务 @Override public void onCreate() { super.onCreate(); } //关闭服务 @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent intent) { Log.e("TAG","onBind************"); yyBinder yyBinder = new yyBinder(); return yyBinder; } class yyBinder extends Binder{ private String name = "zouhao"; public void getName(){ Toast.makeText(getApplicationContext(),"service中的函数"+name,Toast.LENGTH_SHORT).show(); Log.e("TAG","********************"+name+"*********************"); } } }
远程service
从一个应用得到另一个应用的信息
Android为我们提供了一种语言 AIDL (Android interface Definition Language)
AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的APP可以和其他APP交互。
一个进程不能直接访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的 基本单元(int啦,double啦序列化等等的) 并且 有序(左边什么顺序传的右边就得用什么顺序接收) 的通过进程边界。
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号