LocalService

1.定义bind实现类

package com.douzi.servicedemo;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


public class MyService extends Service {

IBinder binder = new MyBinder();

class MyBinder extends Binder{
public MyService getService()
{
return MyService.this;
}
public String getName(String name){
return "你的名字是:"+ name;
}
}

public void helloService(){
for (int i = 0 ; i < 100 ; i++){
try {
Thread.sleep(1000);
Log.i("MyService", i+"");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
helloService();
}
}).start();
return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MyService", "onDestroy");
}


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("MyService", "onUnbind");
return super.onUnbind(intent);
}


}


2.调用

package com.douzi.servicedemo;


import com.douzi.servicedemo.MyService.MyBinder;


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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

private Intent mIntent;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Log.i("MainActivity", "button 01 start...");
// TODO Auto-generated method stub
mIntent = new Intent(MainActivity.this, MyService.class);
startService(mIntent);
}
});

button02.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(mIntent);
}
});

Button button03 = (Button)findViewById(R.id.intentServiceButton);
button03.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent _intent = new Intent(MainActivity.this, MyIntentService.class);
startService(_intent);
}
});

Button button04 = (Button)findViewById(R.id.bindButton);
button04.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent _intent = new Intent(MainActivity.this, MyService.class);
bindService(_intent, conn, Service.BIND_AUTO_CREATE);
}
});


Button button05 = (Button)findViewById(R.id.unbindButton);
button05.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(conn);
}
});
}


private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//String mName=((MyBinder)service).getName("李想");
//Toast.makeText(MainActivity.this, mName, 3000).show();
((MyBinder)service).getService().helloService();
}
};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值