安卓开发之-服务启动方式-绑定方式

流程:

1.创建服务类,重写(ctrl+O快捷键)onCreate方法和onBind对象之后进行相应修改
2.抽取接口
3.0创建Serviceselect类的对象
3.1在服务类中创建Serviceselect类(这是一个内部类),实现接口
4.在清单文件中配置服务
5.在Activity中初始化控件和连接对象
6.在activity中编写绑定服务和编写服务
7.创建自定义服务类,(ctrl+鼠标左键 类似于VS中的F12,可以查询源代码)
8.设置查询响应按钮
9.布局文件中需要设置相关按钮以及相关图形界面

准备工作

创建类和接口:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

代码:
My_BindService类:
package com.example.bluelesson.my_bindservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;


public class My_BindService extends Service {
//3.0创建Serviceselect类的对象
    Serviceselect mServiceselect;
    private List<String> mList = new ArrayList<>();


    //0.构造器
    public My_BindService() {
    }
    //1.创建服务类,重写(ctrl+O快捷键)onCreate方法和onBind对象之后进行相应修改
    //2.见接口那边
    @Override
    public void onCreate() {
        super.onCreate();
        mList.add("张三");
        mList.add("李四");
        mList.add("齐天大圣");
        mList.add("沙和尚");
        mList.add("白龙马");
        mServiceselect = new Serviceselect();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return (IBinder) mServiceselect;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    //3.1在服务类中创建Serviceselect类(这是一个内部类),实现接口
class Serviceselect extends Binder implements  SelectInterface{

        @Override
        public String SelectString(int id) {
            if(id<0&&id>mList.size())
            return "";
            return mList.get(id);
        }
        //下面这个方法可以自己定义
        public void show(){
            showB();
        }


    }
    private  void showB(){

    }

}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluelesson.my_bindservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--4.配置服务:下面为服务配置!!!!!-->
        <service
            android:name=".My_BindService"
             android:enabled="true"
            android:exported="true"></service>
        <!--上面为服务配置!!!!!-->
    </application>

</manifest>
MainActivity类
package com.example.bluelesson.my_bindservice;

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;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //5.在Activity中初始化控件和连接对象
    Intent mIntent;        //创建和关闭服务的Intent对象
    MyServiceConnection mMyServiceConnection;//创建服务连接对象,下面也要创建与之对应的类
    My_BindService.Serviceselect mServiceselect;//自定义Binder对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化对象
        //创建ServiceConnect回调对象
        mMyServiceConnection = new MyServiceConnection();
        mIntent = new Intent(this,My_BindService.class);//服务类的class
    }

//6.在activity中编写绑定服务和编写服务
    public  void onStartService(View view){
        bindService(mIntent,  //指定服务的Intnet对象
                mMyServiceConnection, //服务连接对象
                Context.BIND_AUTO_CREATE   //绑定之后,自动创建服务
        );
    }
    //在布局文件中直接ctrl+左键直接到达这里
public  void onStopService(View view){
        //解除绑定服务对象
    unbindService(mMyServiceConnection);
    //关闭服务
    stopService(mIntent);
}
//7.创建自定义服务类,ctrl+鼠标左键 类似于VS中的F12
class MyServiceConnection implements ServiceConnection{
    //服务启动成功时调用
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        mServiceselect = (My_BindService.Serviceselect)iBinder;
    }
//Android系统在同service的连接意外丢失时调用
    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}
//8.设置查询按钮
    public  void onSelect(View view){
        EditText editText = findViewById(R.id.edit_select);
        String strId = editText.getText().toString();
        if (strId.equals("")) return;
        Integer integer = new Integer(strId);
        String str = mServiceselect.SelectString(integer.intValue());
        if (str.equals("")) return;
        TextView textView = findViewById(R.id.show);
        textView.setText(str);
    }

}
资源文件(主要是布局文件)
<?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:layout_height="match_parent"
    android:orientation= "vertical"
    android:padding="16dp"
    tools:context="com.example.bluelesson.my_bindservice.MainActivity">
    <Button
        android:text="启动服务"
        android:onClick="onStartService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="关闭服务"
        android:onClick="onStopService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_weight="3"
    android:textSize="20sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="查询"
    />

    <EditText
        android:layout_weight="2"
        android:id="@+id/edit_select"
        android:hint="0"
        android:textSize="20sp"
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<Button
    android:layout_weight="3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:onClick="onSelect"
    android:text="查询"/>

</LinearLayout>
    <TextView
        android:id="@+id/show"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
小结

服务有两种启动方式:

startService方式:

该方法启动的服务所在的进程属于服务进程,服务一旦启动服务,服务就跟Activity无关,(注意:启动服务的地方无所谓在哪里!!!,activity和服务乃至广播里面皆可以),这种方式做到了分离界面

bindService(绑定方式):

该方法启动的服务所在进程不属于服务进程,就是说启动服务的地方一定在Activity里面,至于在哪一个Activity无所谓!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值