Android基础知识_绑定Service

一、绑定Service的示例

启动服务可以使用startService这种方式启动,同时启动服务我们还可以使用绑定服务的方式来进行启动。如何绑定呢?请阅读以下代码。

示例工程LearnService MyService.java的代码如下:

package com.example.learnservice;

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

public class MyService extends Service {

    public MyService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

}

示例工程LearnService activity_main.xml的代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

    <Button
        android:id="@+id/btnStopService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止服务" />

    <Button
        android:id="@+id/btnBindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绑定服务" />

    <Button
        android:id="@+id/btnUnbindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解除绑定服务" />

</LinearLayout>

示例工程LearnService MainActivity.java的代码如下:

package com.example.learnservice;

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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener, ServiceConnection{

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /* 服务的实例在操作系统中只有一个
         * Intent用来配置程序要启动的Service信息
         * 故定义一个成员变量intent, 便于startService、stopService使用
         * */
        intent = new Intent(MainActivity.this, MyService.class);

        /* 如果多个按钮都要执行函数, 可以使用this实现事件监听器, 同时让MainActivity实现OnClickListener接口 */
        findViewById(R.id.btnStartService).setOnClickListener(this);
        findViewById(R.id.btnStopService).setOnClickListener(this);
        findViewById(R.id.btnBindService).setOnClickListener(this);
        findViewById(R.id.btnUnbindService).setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        /* 根据被点击View的ID进行判断 */
        switch (v.getId()) {
        case R.id.btnStartService:
            startService(intent);
            break;
        case R.id.btnStopService:
            stopService(intent);
            break;
        case R.id.btnBindService:
            /* 第一个参数: Intent
             * 第二个参数: 服务的连接, 主要用来侦听服务的状态
             * 第三个参数: 常量
             * */
            bindService(intent, this, Context.BIND_AUTO_CREATE);
            break;
        case R.id.btnUnbindService:
            unbindService(this);
            break;
        }
    }
    /* 服务被绑定成功之后执行 */
    @Override
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {
        System.out.println("Service Connected");
    }
    /* 服务所在的进程崩溃或者被杀掉的时候执行 */
    @Override
    public void onServiceDisconnected(ComponentName arg0) {

    }
}

示例工程LearnService AndroidManifest.xml的部分主要代码如下:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.learnservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service 
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" >

        </service>
    </application>

二、程序运行描述

运行程序,点击绑定服务,在LogCat中可以看到”Service Connected“字样,证明服务被绑定成功。解除绑定,则点击解除绑定服务即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值