核心组件之service

这里写图片描述
新建类 echoservice.java 继承service
这里写图片描述
然后 AndroidManifest.xml 中添加
这里写图片描述
这里写图片描述

echoservice.java

package com.itheima.service;

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

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

public class echoservice extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("onBind");
        return echoServiceBinder;
    }
//被final修饰的变量就是一个常量,只能赋值一次
private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();    
public class EchoServiceBinder extends Binder{ // EchoServiceBinder 扩展自Binder
    public echoservice getservice(){
        return echoservice.this;
    }
}

public int getCurrentNumber(){
    return i;
}

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        System.out.println("on Creat");
        starttimer();//起动timer
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        System.out.println("on Destroy");
        stoptimer();//停止timer
        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();
            timer = null;
            task = null;
        }
    }

    private Timer timer=null;
    private TimerTask task=null;

}

MainActivity.java

package com.itheima.service;

import java.net.ContentHandler;

import android.support.v7.app.ActionBarActivity;
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.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends ActionBarActivity implements OnClickListener, ServiceConnection {
private Button btnStartService,btnStopService,btnBindService,btnUnbindService;
private Intent serviceIntent; 
private echoservice  service = null; 
private Button  btnGetCurrentNumber;
@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);
        btnGetCurrentNumber = (Button) findViewById(R.id.btnGetCurrentNumber);

        btnStartService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetCurrentNumber.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
          switch (v.getId()) {
        case R.id.btnstartservice:
            startService(serviceIntent);//开启service
            break;

        case R.id.btnstopservice:
            stopService(serviceIntent);//停止service
            break;

        case R.id.btnBindService:
            bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);//绑定service
            break;

        case R.id.btnUnbindService:
            unbindService(this);
            service = null;
            break;

        case R.id.btnGetCurrentNumber:
            System.out.println("当前数字"+service.getCurrentNumber());
            break;
        }
    }
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {//绑定成功后执行
          System.out.println("onServiceConnected");

          service =((echoservice.EchoServiceBinder) binder).getservice();//强制类型转换
    }
    @Override
    public void onServiceDisconnected(ComponentName name) { //service崩溃时执行
        // TODO Auto-generated method stub

    }  
}

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:orientation="vertical"
    tools:context="com.itheima.service.MainActivity" >

    <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/btnGetCurrentNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get current number" />

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值