Service的肤浅了解

Service就我自己理解就是当你退出程序之后,程序还可以在后台运行的控件
最简单的实现Service方法 就是创建一个类继承Service

发现它跟Activity有点类似 因为Service也有oncreate
onstart ondestory的方法(其他的就没有了)
打开源代码 Service竟然有这么多方法这里写图片描述

额 但是现在用一些最常用的方法 onBind onUnbind
onStartCommand 这些常用的方法

了解一个控件就需要了解这些方法在什么时候执行(我指的是我自己常用的那些)

就我现在所知启动Service有两种方法bindService和StartService

于是就设置4个按钮

<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=".MainActivity">

    <TextView
        android:id="@+id/textshow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show me "/>

    <Button
        android:id="@+id/startservice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="startService"
        />
    <Button
        android:id="@+id/stopservice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="stopService"
        />
    <Button
        android:id="@+id/bindservice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="bindService"
        />
    <Button
        android:id="@+id/unbindservice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="unbindService"
        />

</LinearLayout>

MainActivity

package com.example.administrator.calculator;

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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener{


//    当我们想获取启动的Service实例时,我们可以用到bindService和onBindService方法,它们分别执行了Service中IBinder()和onUnbind()方法。
    private Button startService,stopService,bindService,unbindService;
    private TestService testService;
    private TextView tv;
    private Context mcontext;

    //这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        //当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("MYSERVICEmAIN","onServiceConnected");
            testService = ((TestService.MyBinder)service).getService();
            tv.setText("I am from Service :" + testService.getString());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("MYSERVICEmAIN","onServiceDisconnected");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupView();

    }

    private void setupView() {
        mcontext = MainActivity.this;
        tv = (TextView) findViewById(R.id.textshow);

        startService = (Button) findViewById(R.id.startservice);
        stopService = (Button) findViewById(R.id.stopservice);
        bindService = (Button) findViewById(R.id.bindservice);
        unbindService = (Button) findViewById(R.id.unbindservice);

        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
;    }

    @Override
    public void onClick(View v) {
        Intent i;
        if(v==startService){
            i = new Intent(MainActivity.this,TestService.class);
            mcontext.startService(i);
        }else if(v==stopService){
            i = new Intent(MainActivity.this,TestService.class);
            mcontext.stopService(i);
        }else if(v==bindService){
            i = new Intent(MainActivity.this,TestService.class);
            mcontext.startService(i);
            mcontext.bindService(i,mServiceConnection,BIND_AUTO_CREATE);
        }else{
            mcontext.unbindService(mServiceConnection);
        }
    }
}

TestService.java

package com.example.administrator.calculator;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.format.Time;
import android.util.Log;

/**
 * Created by Administrator on 2015/11/21.
 */
public class TestService extends Service{
//   定义一个Tag标签
    private static final String TAG = "MYSERVICE";
//    这里定义一个Binder 用在onBind()方法,这样Activity那边好、可以获取到
    private  MyBinder myBinder = new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG,"onBind");
        return myBinder;
    }

    @Override
    public void onCreate() {
        Log.e(TAG,"onCreate");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG,"onStart");
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        Log.e(TAG,"onDestroy");
        super.onDestroy();
    }

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

    public String getString(){
        return "获取到Service实例";
    }

    public class MyBinder extends Binder{
        TestService getService(){
            return TestService.this;
        }
    }


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

按顺序按按钮
这里写图片描述

结果

这里写图片描述

今天就到此为止

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值