Android之路service
Service服务,运行于后台。使用方法1:startService
1、 编写一个类继承service
2、 复写方法onStartCommand、onBind、onCreate、onDestroy
3、 在mainfest中声明服务
4、 启动服务
5、 关闭服务
用户推出后服务依然在运行,mainactivity。Java:
public class MainActivity extends Activity
{
private Button btnStartService;
private Button btnStopService;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button)findViewById(R.id.btn1);
btnStopService = (Button)findViewById(R.id.btn2);
btnStartService.setOnClickListener(listener);
btnStopService.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, ExampleService.class); //通过intent传递参数
Button btn = (Button)v;
// TODO Auto-generated method stub
switch(btn.getId())
{
case R.id.btn1:
startService(intent);//启动服务
break;
case R.id.btn2:
stopService(intent);//关闭服务
break;
default:
break;
}
}
};
}
ExampleService.java:
public class ExampleService extends Service
{
/* (non-Javadoc)
* @see android.app.Service#onCreate()
*/
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
/* (non-Javadoc)
* @see android.app.Service#onDestroy()
*/
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
/* (non-Javadoc)
* @see android.app.Service#onStartCommand(android.content.Intent, int, int)
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
运行结果
点击start开启服务,然会点击右侧的返回按钮,在系统里查看正在运行的服务
Bound Service:允许其他组件绑定到这个service,可以发送请求,也可以接受请求。Bound service仅仅在服务于其他组件时存在,不能独自无期限的存在后台。
创建bound service,必须提供一个binder对象,客户端使用这个对象与服务交互,有三种定义方式:
1、 扩展binder类(进程内交互)
A、在service类中创建一个binder实例,包含客户端能调用的公共方法,返回当前服务对象
B、在onBind方法中返回binder实例
C、在客户端从onServiceConnected方法中获取binder实例
2、 使用messenger(进程间交互)
3、 使用AIDL(android interface delinition language)(进程间交互)
ExampleService.Java:
public class ExampleService extends Service
{
public static String TAG = null;
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder
{//创建了binder实例,返回当前服务对象
public ExampleService getService()
{
return ExampleService.this;
}
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return binder;// 返回来binder实例
}
public void method()
{//客户端可以调用的一个公共的方法
Log.i(TAG, "method()");
}
}
Mainactivitu.java:
public class MainActivity extends Activity
{
private Button btnStartService;
private Button btnStopService;
private static Boolean isConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button)findViewById(R.id.btn1);
btnStopService = (Button)findViewById(R.id.btn2);
btnStartService.setOnClickListener(listener);
btnStopService.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
Button btn = (Button)v;
// TODO Auto-generated method stub
switch(btn.getId())
{
case R.id.btn1:
bindsv(); //开启服务
break;
case R.id.btn2:
onbind(); //关闭服务
break;
default:
break;
}
}
};
private void bindsv()
{
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, ExampleService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
//开启
}
private void onbind()
{
// TODO Auto-generated method stub
if(isConnected)
{
unbindService(conn); //关闭
}
}
private ServiceConnection conn = new ServiceConnection()
{//内部类,实现conn接口
@Override
public void onServiceDisconnected(ComponentName name)
{
// TODO Auto-generated method stub
isConnected = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder)
{
// TODO Auto-generated method stub
MyBinder mybinder = (MyBinder)binder;//获取binder
ExampleService service = mybinder.getService();//提取服务
service.method();//调用服务的方法
isConnected = true;
}
};
@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;
}
}