Android中Service的简单使用

先创建个类,继承于Service类,例如:
EchoService.java文件:

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

//启动一个service时,先运行onCreate(),然后运行onBind(Intent),其中onBind(Intent)需要返回一个Binder对象实例,下面是构造一个实例

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("onDestory");

        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=null;
    private TimerTask task=null;
}

MainActivity.java文件:

public class MainActivity extends Activity implements OnClickListener, ServiceConnection {  //继承接口ServiceConnection,重写onServiceConnected()方法与service实现通信
    private Button btnStartService,btnStopService,btnBindService,btnUnbindService,btnGetCurrentNumber;
    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);
        btnGetCurrentNumber = (Button) findViewById(R.id.btnGetCurrentNum);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.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("service的信息"+echoService.getCurrentNum());//调用下面获取的echoService来访问其内部方法,达到获取service信息的目的
            }
            break;
        }
    }
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        System.out.println("onServiceConnected");
        echoService = ((EchoService.EchoServiceBinder)binder).getService();//获取service
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值