关闭

Android中Service的简单使用

标签: androidservice
170人阅读 评论(0) 收藏 举报
分类:

先创建个类,继承于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) {
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8887次
    • 积分:356
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:1篇
    • 译文:1篇
    • 评论:1条
    个人博客
    文章分类
    最新评论