Service 服务 四大组件之一

Service的启动方式以及生命周期

public class MyService extends Service {
    private static final String TAG = "MyService";

    public MyService() {
        Log.i(TAG, "MyService: 不好..被创建了");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: 绑啥绑");
        return null;
    }
}

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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/create_server_id"
        android:text="启动一个服务"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stop_server_id"
        android:text="停止一个服务"
        />
</LinearLayout>

package com.example.month2to2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.month2to2.service.MyService;

public class MainActivity extends AppCompatActivity {
    private Button createServerId;
    private Button stopServerId;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        stopServerId = findViewById(R.id.stop_server_id);
        createServerId = findViewById(R.id.create_server_id);

        stopServerId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });

        createServerId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
    }
}

绑定服务和解除绑定

public class MainActivity extends AppCompatActivity {
    private Intent intent;//开启服务的意图对象
    private MyService myService;//通过绑定获取Service
    //绑定服务连接
    private ServiceConnection connection=new ServiceConnection() {
       @Override
       public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: ");
            MyService.MyBinder binder = (MyService.MyBinder) service;
            binder.callShow();
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.绑定服务
        intent =new Intent(this,MyService.class);
        //参数一:intent意图  参数二:绑定服务连接  参数三:flag标记BIND_AUTO_CREATE 绑定的时候自动创建服务
        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
    //2.解除绑定服务
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
    //3.点击结婚按钮调用服务的结婚方法
    public void jiehun(View view){
        myService.jiehun();
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值