服务在后台默默地运行,是看不见的
一 startService
1 步骤
1 创建一个类继承Service (这个类就是服务了)
在这里重写一些方法(用来测试)
2 在清单文件中注册Service
<!--组件必须在清单文件中注册才能生效-->
<service android:name=".MyService"/>
3 xml写2个按钮,添加两个点击事件
4 点击事件,分别写启动服务和停止服务
startService(new Intent(this,MyService.class
));
stopService(new Intent(this,MyService.class
));
2 代码
1 创建一个类继承Service (这个类就是服务了)
MyService.java
package com.example.myactivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("cs","onCreate()");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("cs","onStart()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("cs","onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("cs","onDestroy()");
}
}
2 在清单文件中注册Service
3 xml写2个按钮,添加两个点击事件
<?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"
tools:context=".MainActivity3">
<Button
android:text="start service"
android:onClick="startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="stop service"
android:onClick="stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
4 点击事件,分别写启动服务和停止服务
package com.example.myactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
//点击事件
//启动服务
public void startService(View view) {
startService(new Intent(this,MyService.class
));
}
//停止服务
public void stopService(View view) {
stopService(new Intent(this,MyService.class
));
}
}
5 startService的生命周期
上述代码执行的效果
生命周期
1 启动服务时
onCreate()
onStartCommand()
onStart()
2 停止服务时
onDestroy()
二 bindService
1 步骤
1 创建MyService继承Service,重写onBind,onUnbind 方法
2 MainActivity里两个按钮点击事件
bindService(new Intent(this,MyService.class),connection, Context.BIND_AUTO_CREATE);
unbindService(connection);
定义MainActivity与MyService的桥梁ServiceConnection
2 代码
activity_main3.xml
<?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=".MainActivity3">
<Button
android:text="start service"
android:onClick="startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="stop service"
android:onClick="stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="bind service"
android:onClick="bindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="unbind service"
android:onClick="unbindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MyService.java
package com.example.myactivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service{
@Override
public void onCreate() {
super.onCreate();
Log.e("cs","onCreate()");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("cs","onStart()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("cs","onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("cs","onDestroy()");
}
// bindService
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("cs","onBind()");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("cs","onUnbind()");
return super.onUnbind(intent);
}
}
MainActivity3.java
package com.example.myactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
//点击事件
//启动服务
public void startService(View view) {
startService(new Intent(this,MyService.class
));
}
//停止服务
public void stopService(View view) {
stopService(new Intent(this,MyService.class
));
}
//点击事件
public void bindService(View view) {
bindService(new Intent(this,MyService.class),connection, Context.BIND_AUTO_CREATE);
}
public void unbindService(View view) {
unbindService(connection);
}
//MainActivity 与 MyService 的桥梁
private ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
3 bindService的生命周期
上述代码执行的效果