Service的一个例子,用来学习Service相当不错。

TestServiceActivity.java

package com.wanghao.service;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestServiceActivity extends Activity {
    private Button startServiceButton = null;
    private Button stopServiceButton = null;
    private Button bindServiceButton = null;
    private Button unBindServiceButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startServiceButton = (Button) findViewById(R.id.startService);
        startServiceButton.setOnClickListener(new StartServiceListener());
        stopServiceButton = (Button) findViewById(R.id.stopService);
        stopServiceButton.setOnClickListener(new StopServiceListener());
        bindServiceButton = (Button)findViewById(R.id.bindService);
        bindServiceButton.setOnClickListener(new BindServiceListener());
        unBindServiceButton = (Button)findViewById(R.id.unBindService);
        unBindServiceButton.setOnClickListener(new UnBindServiceListener());
        
        System.out.println("Activity onCreate");
    }

    // 启动Service监听器
    class StartServiceListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(TestServiceActivity.this, MyService.class);
            startService(intent);
        }
    }

    // 停止Service监听器
    class StopServiceListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(TestServiceActivity.this, MyService.class);
            stopService(intent);
        }
    }

    // 连接对象
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("Bind success!");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("UnBind success!");
        }
    };
    
    // 绑定Service监听器
    class BindServiceListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(TestServiceActivity.this, MyService.class);
            bindService(intent, conn, Service.BIND_AUTO_CREATE);
        }
    }
  
    // 解除绑定Service监听器
    class UnBindServiceListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(TestServiceActivity.this, MyService.class);
            unbindService(conn);
        }
    }
}


服务类MyService.java

package com.wanghao.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

    // 必须实现的一个方法,返回一个绑定的接口给Service
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service onBind");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("Service onUnBind");
        return super.onUnbind(intent);
    }

    // 当Service第一次被创建时,由系统调用
    @Override
    public void onCreate() {
        System.out.println("Service onCreate");
        super.onCreate();
    }

    // 当Service不再使用,系统调用该方法
    @Override
    public void onDestroy() {
        System.out.println("Service onDestroy");
        super.onDestroy();
    }

    // 当通过startService()方法启动Service时,该方法被调用,API level 5之后建议复写onStartCommand()方法
    @Override
    public void onStart(Intent intent, int startId) {
        System.out.println("Service onStart");
        super.onStart(intent, startId);
    }

    // Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("Service onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值