android四大组件之活动服务

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

转瞬即逝的记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值