Android Service 服务的基本用法

来源:互联网 发布:网站数据采集器 编辑:程序博客网 时间:2024/06/05 05:14

Service 服务的基本用法

创建&绑定服务
活动与服务进行通信


创建&绑定服务

活动与服务进行通信

创建四个按钮:

<?xml version="1.0" encoding="utf-8"?><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:orientation="vertical"    tools:context="com.example.scott.servicetest.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/start_service"        android:text="Start Service"        android:textAllCaps="false"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/stop_service"        android:text="Stop Service"        android:textAllCaps="false"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/bind_service"        android:text="Bind Service"        android:textAllCaps="false"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/unbind_service"        android:text="Unbind Service"        android:textAllCaps="false"/></LinearLayout>
package com.example.scott.servicetest;import android.content.ComponentName;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.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button startService;    private Button stopService;    private Button bindService;    private Button unbindService;    private Intent intent;//不能这么写Intent intent = new Intent(MainActivity.this,MyService.class);    private MyService.DownloadBinder downloadBinder;//创建一个SerciceConnection的匿名类并重写下面的两个方法,这两个方法分别会在活动与服务成功绑定和解除绑定的时候调用    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {        //向下转型获取DownloadBinder实例            downloadBinder = (MyService.DownloadBinder) service;            downloadBinder.startDownload();            downloadBinder.getProgress();        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //~~~~~        intent = new Intent(MainActivity.this,MyService.class);        startService = (Button) findViewById(R.id.start_service);        stopService = (Button) findViewById(R.id.stop_service);        bindService = (Button) findViewById(R.id.bind_service);        unbindService = (Button) findViewById(R.id.unbind_service);        startService.setOnClickListener(this);        stopService.setOnClickListener(this);        bindService.setOnClickListener(this);        unbindService.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case  R.id.start_service:                startService(intent);                break;            case R.id.stop_service:                stopService(intent);                break;            case R.id.bind_service:   //这里传入的BIND_AUTO_CREATE表示在活动和服务进行绑定的时候会自动执行onCreate(),不会执行onStartCommand()             bindService(intent,connection,BIND_AUTO_CREATE);                break;            case R.id.unbind_service:                unbindService(connection);                break;            default:                break;        }    }}

总结一下,onCreate()方法是在服务第一次创建的时候调用,onStartCommand()方法是在每次启动服务的时候调用。
活动与服务进行通信:在MyService中创建一个继承自Binder的实例;然后在onBind()中返回这个实例。然后再活动中绑定服务,就可以服务里的Binder提供的方法了。

关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。
这里写图片描述

0 0
原创粉丝点击