第14天-01-服务简介

今天来谈一下服务。停,别胡思乱想,不是你想象的那种服务。。。。
首先新建一个项目,命名为ServiceDemo。
在项目名下新建一个类,命名为MyService让他继承Service。
1.项目结构
在这里插入图片描述
2.代码块
mainactivity:

package com.glsite.servicedemo;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private Intent mIntent;
    private ServiceConnection mConnection= new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            mService = ((MyService.MyBinder) service).getService();
            System.out.println("service调用成功");
            mService.execute();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;

        }
    };
    private MyService mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIntent = new Intent(this, MyService.class);
       // startService(mIntent);
        bindService(mIntent,mConnection, Context.BIND_AUTO_CREATE);
    }

    public void stop(View view) {
        if (mIntent != null) {
            stopService(mIntent);
        }
    }

    public void unbind(View view) {
        unbindService(mConnection);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }
}

myservice:

package com.glsite.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class MyService extends Service {
    
    private IBinder mBinder = new MyBinder();
    
    
    public MyService() {
        System.out.println("maservice constructor");
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onbind");
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("创建了");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //zhe这一句必须写在return的上面,否则访问不到
        System.out.println("饿呢恩");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("销毁了");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("解绑");
        return super.onUnbind(intent);
    }
    public class MyBinder extends Binder{
        MyService getService(){
            return MyService.this;
        }
    }
    public void  execute(){
        System.out.println("通过Binder得到severe的引用来调用service内部的方法");
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="停止服务"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="96dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="解绑服务"
        android:onClick="unbind"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

</android.support.constraint.ConstraintLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值