Android Service两种启动方式

  两种启动service的方式:
这里写图片描述
  
  ok,可以看出,service的生命周期为:
  onCreate(),onStartCommand(),onBind(),onUnbind(),onDestory()

  下面我们来直接上代码验证。
  
  布局:

<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"
    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" >

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

    <Button
        android:id="@+id/bn_start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="startService" />
    <Button
        android:id="@+id/bn_stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="stopService" />

    <Button
        android:id="@+id/bn_bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="bindService" />
    <Button
        android:id="@+id/bn_unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="unBindService" />

</LinearLayout>

  Service类:
  

package com.kevin.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 Binder binder=new Binder();
    @Override
    public IBinder onBind(Intent intent)
    {
        Log.d("", "onBind--");
        return binder;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d("", "onCreate--");
    }

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

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d("", "onDestroy--");
    }

    @Override
    public boolean onUnbind(Intent intent)
    {
        Log.d("", "onUnbind--");
        return super.onUnbind(intent);
    }

}

Activity类:

package com.kevin.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.kevin.service.MyService;

public class MainActivity extends Activity implements OnClickListener
{

    private Button bnStart, bnBind, bnStop, bnUnBind;

    private Binder binder;
    private ServiceConnection conn = new ServiceConnection()
    {

        @Override
        public void onServiceDisconnected(ComponentName name)// 这个方法一般不会被调用,只有Service被破坏了或者被杀死的时候才会被调用
        {
            binder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            binder = (Binder) service;
        }
    };
    private Intent intent;

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

    private void initViews()
    {
        bnStart = (Button) findViewById(R.id.bn_start_service);
        bnStop = (Button) findViewById(R.id.bn_stop_service);
        bnBind = (Button) findViewById(R.id.bn_bind_service);
        bnUnBind = (Button) findViewById(R.id.bn_unbind_service);
        bnStart.setOnClickListener(this);
        bnBind.setOnClickListener(this);
        bnStop.setOnClickListener(this);
        bnUnBind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        intent = new Intent(this, MyService.class);
        if (v == bnStart)
        {
            startService(intent);// 正常启动
        } else if (v == bnBind)
        {
            bindService(intent, conn, BIND_AUTO_CREATE);// 绑定启动
        } else if (v == bnStop)
        {
            stopService(intent);// 正常结束
        } else if (v == bnUnBind)
        {
            unbindService(conn);// 解除绑定
        }
    }
}

注册service:

 <service android:name="com.kevin.service.MyService" >
        </service>

好了,看效果吧。
这里写图片描述

先看下正常启动service。
点击startService按钮:
这里写图片描述

点击stopService按钮:
这里写图片描述

再看下绑定启动service:
点击bindService按钮:
这里写图片描述

点击unBindService按钮:
这里写图片描述

如果既点击了startService按钮,又点击了bindService按钮(不论点击顺序),需要点击stopService和unBindService两个按钮才能结束服务。

源码下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值