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();
}
};
}
- 抽象、接口、内部类
- 二叉树的重建,遍历,求深度算法
- 【Linux网络编程】浅谈 C/S 和 B/S 架构
- POJ 3630
- WinGW——配置sublime 3运行C程序
- Android , 浅谈显示Intent和隐式Intent
- centos6.5+Django+mysql+nginx+uwsgi
- libevent demo详细分析(对比epoll)
- VS2008“无法找到“xxx.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。”
- 畅通工程续
- Sqlserver_In、exists使用
- 使用WebView弹出不支持localStoragede对话框的问题
- Javascript常用前台表单验证
- 我的第一个项目——说说那些年我们一起写过的代码
- Android硬件抽象层(HAL)概要介绍和学习计划
- 2016年美的校招面试
- 利用SurfaceView实现转盘抽奖
- 应用宝SDK接入总结
- 重新学习C# 之 配置C#命令行编译器
- 我的第一个项目——说说那些年我们一起写过的代码