Android之Service复习深入,vsandroid开发

}

public void showNumber(){

new Thread(new Runnable() {

int i=1;

@Override

public void run() {

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(i++);

}

}

}).start();

}

}

HelloServiceActivity.java

package hb.android.service;

import hb.android.service.HelloService.HelloBinder;

import android.app.Activity;

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.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class HelloServiceActivity extends Activity {

Intent intent;

boolean flag = true;

HelloService mService;;

boolean mBound = false;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

intent = new Intent();

initMyWidget();

setMyWidgetListener();

}

public void setMyWidgetListener() {

MyWidget.btn_bind.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_start.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_stop.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_unbind.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_kill.setOnClickListener(new WidgetOnClickListener());

}

class WidgetOnClickListener implements OnClickListener {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_start:

intent.setClass(getApplicationContext(), HelloService.class);

intent.putExtra(“test”, “start”);

startService(intent);

flag = true;

MyWidget.btn_stop.setEnabled(flag);

break;

case R.id.btn_stop:

intent.putExtra(“test”, “stop”);

stopService(intent);

flag = false;

MyWidget.btn_stop.setEnabled(flag);

break;

case R.id.btn_kill:

android.os.Process.killProcess(android.os.Process.myPid());

break;

case R.id.btn_bind:

Intent intent = new Intent(getApplicationContext(), HelloService.class);

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//自动调用Service里的onCreate方法

System.out.println(“bind:”+mService);

opeService();

break;

case R.id.btn_unbind:

if (mBound) {

unbindService(mConnection);//自动调用Service的onDestory方法

mBound = false;

}

break;

default:

break;

}

}

public void opeService() {

System.out.println(“opeService:”+mService);

// mService.showNumber();

}

}

public void initMyWidget() {

MyWidget.btn_bind = (Button) findViewById(R.id.btn_bind);

MyWidget.btn_start = (Button) findViewById(R.id.btn_start);

MyWidget.btn_stop = (Button) findViewById(R.id.btn_stop);

MyWidget.btn_unbind = (Button) findViewById(R.id.btn_unbind);

MyWidget.tv_test = (TextView) findViewById(R.id.tv_test);

MyWidget.btn_kill = (Button) findViewById(R.id.btn_kill);

}

private static class MyWidget {

static Button btn_start;

static Button btn_stop;

static Button btn_bind;

static Button btn_unbind;

static Button btn_kill;

@SuppressWarnings(“unused”)

static TextView tv_test;

}

@Override

protected void onDestroy() {

super.onDestroy();

stopService(intent);

}

/** Defines callbacks for service binding, passed to bindService() */

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,

IBinder service) {

// We’ve bound to LocalService, cast the IBinder and get LocalService instance

HelloBinder binder = (HelloBinder) service;

mService = binder.getService();

System.out.println(“ServiceConnection:”+mService);

mBound = true;

mService.showNumber();

}

@Override

public void onServiceDisconnected(ComponentName arg0) {

mBound = false;

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

}

};

@Override

protected void onStop() {

super.onStop();

if (mBound) {

unbindService(mConnection);

mBound = false;

}

};

}

注意:当服务创建之后由,onServiceConnected 这个方法负责建立与Servicer的连接

意思当Activit用BindService启动服务之后,由:

public ServiceConnection mConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

System.out.println(“onServiceConnected”);

mBound = false;

}

/**

  • 服务创建之后收些方法建立Serivce与Activity的连接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值