栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Service基础(启动服务与绑定服务)(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Service基础(启动服务与绑定服务)(一)

     Service是Android中一个类,它是Android 四大组件之一,使用Service可以在后台执行耗时的操作(注意需另启子线程),其中Service并不与用户产生UI交互。其他的应用组件可以启动Service,即便用户切换了其他应用,启动的Service仍可在后台运行。一个组件可以与Service绑定并与之交互,甚至是跨进程通信。通常情况下Service可以在后台执行网络请求、播放音乐、执行文件读写操作或者与contentprovider交互等。

  本文主要讲述service服务里的启动与绑定服务。

首先,XML程序如下:




    

创建一个service类,并写创建、启动、绑定、摧毁、解绑五个方法

代码如下:

package com.example.service;

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

public class MyService extends Service {

    private int i;
    public MyService() {
    }
    //创建
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG","服务创建了");
    }

    class MyBinder extends Binder {
        //定义自己需要的方法(实现进度监控)
        public int getProcess(){
            return i;
        }
    }


    //启动方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TAG","服务启动了");
        return super.onStartCommand(intent, flags, startId);
    }

    //解绑
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG","服务解绑了");
        return super.onUnbind(intent);
    }

    //摧毁
    @Override
    public void onDestroy() {
        Log.e("TAG","服务摧毁了");
        super.onDestroy();
    }

    //绑定方法
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       // throw new UnsupportedOperationException("Not yet implemented");
        Log.e("TAG","服务绑定了");
        return new MyBinder();
    }
}

然后在MainActivity里面写点击启动方法:

public void operate(View v) {
        switch (v.getId()){
            case R.id.start:
                //启动服务 :创建——启动——摧毁
                //如果服务已经创建了,后续重复启动,操作的都是同一个服务,不回在创建新 的服务,除非先摧毁他
                Intent it1=new Intent(this,MyService.class);
                startService(it1);
                break;
            case R.id.stop:
                Intent it2=new Intent(this,MyService.class);
                stopService(it2);
                break;
        }
    }

然后写绑定的方法:

注意:捆绑和解绑的对象应该是同一个对象,如果捆绑与解绑的对象不一样,则会报如下的错误:

Service not registered: 
com.m1910.servicetest.MainActivity$1@41ddfcc0

本篇文章捆绑与解绑的对象都是conn,定义一个全局变量:

    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

然后写解绑与捆绑的方法:

case R.id.bind:
                //绑定服务
                Intent it3=new Intent(this,MyService.class);
                bindService(it3,conn,BIND_AUTO_CREATE);
                break;

            case R.id.unbind:
                //解绑服务
//                unbindService(conn);
//                if (isBound) {
//                    unbindService(conn);// 解绑服务
//                    isBound = false;
//                }
                    unbindService(conn);//解绑服务
                break;

完整的程序如下:

package com.example.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private boolean isBound = false;

    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

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

    public void operate(View v) {
        switch (v.getId()){
            case R.id.start:
                //启动服务 :创建——启动——摧毁
                //如果服务已经创建了,后续重复启动,操作的都是同一个服务,不回在创建新 的服务,除非先摧毁他
                Intent it1=new Intent(this,MyService.class);
                startService(it1);
                break;
            case R.id.stop:
                Intent it2=new Intent(this,MyService.class);
                stopService(it2);
                break;
            case R.id.bind:
                //绑定服务
                Intent it3=new Intent(this,MyService.class);
                bindService(it3,conn,BIND_AUTO_CREATE);
                break;

            case R.id.unbind:
                //解绑服务
//                unbindService(conn);
//                if (isBound) {
//                    unbindService(conn);// 解绑服务
//                    isBound = false;
//                }
                    unbindService(conn);//解绑服务
                break;

        }
    }
}

Service基础(实现IBinder)(二)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/681177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号