核心组件之service

来源:互联网 发布:miss淘宝店网址 编辑:程序博客网 时间:2024/06/06 00:04

这里写图片描述
新建类 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>
原创粉丝点击