服务在后台默默地运行,是不可见的。
新建一个Activity
在清单文件中设置Activity3默认启动
MyService.java
package com.dingjiaxiong.myactivitytiaozhuan;
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("myservice", "onCreate: " );
}
//已过期
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("myservice", "onStart: " );
}
//替代onstart
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("myservice", "onStartCommand: " );
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("myservice", "onBind: " );
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("myservice", "onUnbind: " );
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.e("myservice", "onDestroy: " );
super.onDestroy();
}
}
在清单文件中注册这个服务
布局更改
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService对应stopService区域"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService"
android:onClick="startService"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopService"
android:onClick="stopService"
/>
LinearLayout>
LinearLayout>
启动和停止服务
package com.dingjiaxiong.myactivitytiaozhuan;
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));
}
}
运行
返回桌面,服务不会停止
点击停止服务按钮后,服务才进行销毁
布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService 对应 unBindService 区域"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService"
android:onClick="bindService"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unbindService"
android:onClick="unbindService"
/>
LinearLayout>
建立桥梁连接服务
bind与unbind
public void bindService(View view){
// connecton 桥梁
bindService(new Intent(this,MyService.class),connecton, Context.BIND_AUTO_CREATE);
}
public void unbindService(View view){
unbindService(connecton);
}
//定义桥梁
private ServiceConnection connecton = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
点击解绑
一般的写法,当此activity被销毁时,会自动解绑服务