android四大组建之Service

Service组件
后台运行的程序
要在AndroidManifest.xml里面配置
<service android:name=".EchoService"></service>
MainActivity添加代码
Intent serviceIntent = new Intent(this,EchoService.class);
startService(serviceIntent);
----------------------
onBind 返回IBinder对象,不为null才能connect成功
同时执行Unbind ,Stop 才能关闭service
Bind Service后退,执行onDestroy
Start Service后退,不执行onDestroy
Service之间通信要先绑定

service 是系统组建,由操作系统控制,只能通过bindService请求操作系统,自己不能create

程序实现了启动服务,每隔一秒输出一个数字。可以start,stop,bind,Unbind,通过logcat查看

MainActivity

package com.sammer.l003usingservice;

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.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

    private Button btnStartService,btnStopService,btnBindService,btnUnbindService,btnGetCurrentNum;
    private Intent serviceIntent;
    private  EchoService echoService = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceIntent = new Intent(this,EchoService.class);

        btnStartService = (Button) findViewById(R.id.btnStartService);
        btnStopService = (Button) findViewById(R.id.btnStopService);
        btnBindService = (Button) findViewById(R.id.btnBindService);
        btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
        btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum);


        btnStartService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetCurrentNum.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnStartService:
                startService(serviceIntent);
                break;
            case R.id.btnStopService:
                stopService(serviceIntent);
                break;
            case R.id.btnBindService:
                bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnUnbindService:
                unbindService(this);
                echoService = null;
                        break;
            case R.id.btnGetCurrentNum:
                if(echoService != null)
                {
                    System.out.println("当前服务中数字时:"+echoService.getCurrentNum());

                }
                break;
        }

    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder binder) {
        System.out.println("onServiceConnected");
//获取当前服务实例
        echoService = ((EchoService.EchoServiceBinder) binder).getService();

    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}
EchoService.java
package com.sammer.l003usingservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/8/9.
 * Weidong Wang 2017
 */

public class EchoService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onBind");
        return echoServiceBinder;
    }

    //自定义一个内部类EchoServiceBinder继承Binder,必须有返回值才能connect
    private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();
    public   class EchoServiceBinder extends Binder {
        public EchoService getService(){
            return  EchoService.this;
        }
    }
    public  int getCurrentNum(){
        return  i;
    }

    @Override
    public void onCreate() {
        System.out.println("onCreate");

        startTimer();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        System.out.println("onDestroy");

        stopTimer();

        super.onDestroy();
    }

    private  int i = 0;
    public void startTimer(){
        if(timer == null){
            timer = new Timer();
            task = new TimerTask(){
                @Override
                public void run() {
                    i++;
                    System.out.println(i);

                }
            };
            timer.schedule(task,1000,1000);
        }
    }
    public void  stopTimer(){
        if(timer != null){
            task.cancel();
            timer.cancel();
            task = null;
            timer = null;
        }
    }

    private Timer timer;
    private TimerTask task;

}
AndroidManifest.xml添加
 </activity>
    <service android:name=".EchoService"></service>
</application>
activity_main
    <Button
        android:id="@+id/btnStartService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service" />

    <Button
        android:id="@+id/btnStopService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Service" />

    <Button
        android:id="@+id/btnBindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bind Service" />

    <Button
        android:id="@+id/btnUnbindService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unbind Service" />

    <Button
        android:id="@+id/btnGetCurrentNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Current Number" />
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值