android四大组件之活动服务
参考地址:https://www.cnblogs.com/li1010425/p/6076066.html
首先需要继承Service
作为服务生命周期主要有:
onCreate() onStart() onBind() onUnBind() onDestroy()
创建时 -》 启动时 -》 绑定时 -》 解绑时 -》 销毁时
package dev.bio.shp.com.hello;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
/**
* @CreateBy: Administrator
* @Version: 1.0
* @Description: TODO ${description}
* @CreateTime: 2021/2/12 12:52
* @PackageName: dev.bio.shp.com.hello
* @ProjectName: hello
*/
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
//startService()bindService()都会执行onCreate()
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this,R.raw.music);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mediaPlayer.start();
}
@Override
public IBinder onBind(Intent intent) {
mediaPlayer.start();
return null;
}
@Override
public boolean onUnbind(Intent intent) {
mediaPlayer.stop();
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
}
}
新建一个类,继承一Activety
package dev.bio.shp.com.hello;
import android.app.Activity;
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.util.Log;
import android.view.View;
import android.widget.Button;
/**
* @CreateBy: Administrator
* @Version: 1.0
* @Description: TODO ${description}
* @CreateTime: 2021/2/11 18:21
* @PackageName: dev.bio.shp.com.hello
* @ProjectName: hello
*/
public class SecondActivity extends Activity {
//定义服务链接
final ServiceConnection connection= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
//创建时执行
@Override
protected void onCreate(Bundle savedInstanceState) {
//创建
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.second);
initView();
}
//初始化
private void initView() {
//绑定按钮
Button bStart = findViewById(R.id.start);
Button bStop = findViewById(R.id.stop);
Button bBind = findViewById(R.id.bind);
Button bUnbind = findViewById(R.id.unbind);
//设置监听
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MusicService.class);
//启动
if(v.getId()==R.id.start){
startService(intent);
Log.d("日志","开始播放");
}
//结束
if(v.getId()==R.id.stop){
stopService(intent);
Log.d("日志","结束播放");
}
//绑定
if(v.getId()==R.id.bind){
//链接connection
//自动创建Context.BIND_AUTO_CREATE
bindService(intent,connection, Context.BIND_AUTO_CREATE);
Log.d("日志","绑定");
}
//解绑
if(v.getId()==R.id.unbind){
unbindService(connection);
Log.d("日志","解绑");
}
}
};
//绑定监听
bStart.setOnClickListener(onClickListener);
bStop.setOnClickListener(onClickListener);
bBind.setOnClickListener(onClickListener);
bUnbind.setOnClickListener(onClickListener);
//模拟点击
onClickListener.onClick(bStart);
}
}
新建一个布局界面对应这个类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/startimage"
android:orientation="vertical">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启服务"
android:textColor="#FFFFFFFF" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭服务"
android:textColor="#FFFFFFFF" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
android:textColor="#FFFFFFFF" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束绑定"
android:textColor="#FFFFFFFF" />
</LinearLayout>