android service详解中怎么启动关闭Service及功能解释

Android中怎么启动关闭Service及功能解释_百度知道
Android中怎么启动关闭Service及功能解释
其他类似问题
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Android中BindService方式使用的理解(转) - Hi, Sun - ITeye技术网站
博客分类:
最近学习了一下Android里面的Service的应用,在BindService部分小卡了一下,主要是开始没有彻底理解为什么要这么实现。
BindService和Started Service都是Service,有什么地方不一样呢:
1. Started Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行【onCreate()-
&onStartCommand()-&startService()-&onDestroy()】,注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。
2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()-&onBind()-&onUnbind()-&onDestroy()】。
调用者Activity:
MainAcitvity
package com.zys.
import com.zys.service.BindService.MyB
import android.R.
import android.app.A
import ponentN
import android.content.C
import android.content.I
import android.content.ServiceC
import android.os.B
import android.os.IB
import android.view.V
import android.view.View.OnClickL
import android.widget.B
public class MainActivity extends Activity {
private Button startB
private Button stopB
private boolean
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flag = false;
startBtn = (Button)this.findViewById(R.id.startBtn);
stopBtn = (Button)this.findViewById(R.id.stopBtn);
startBtn.setOnClickListener(listener);
stopBtn.setOnClickListener(listener);
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startBtn:
bindService();
case R.id.stopBtn:
private void bindService(){
Intent intent = new Intent(MainActivity.this,BindService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
private void unBind(){
if(flag == true){
unbindService(conn);
flag = false;
private ServiceConnection conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder binder = (MyBinder)
BindService bindService = binder.getService();
bindService.MyMethod();
flag = true;
服务BindService
BindService
package com.zys.
import java.io.FileD
import android.app.S
import android.content.I
import android.os.B
import android.os.IB
import android.os.II
import android.os.P
import android.os.RemoteE
import android.util.L
public class BindService extends Service {
private static final String TAG = "BindService";
public void MyMethod(){
Log.i(TAG, "BindService--&MyMethod()");
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return myB
public class MyBinder extends Binder{
public BindService getService(){
return BindService.this;
private MyBinder myBinder = new MyBinder();
由于Android 中的Service使用了onBind 的方法去绑定服务,返回一个Ibinder对象进行操作,而我们要获取具体的Service方法的内容的时候,我们需要Ibinder对象返回具体的Service对象才能操作,所以说具体的Service对象必须首先实现Binder对象,这个样子的话我们才能利用bindService的方法对Service进行绑定,获取Binder对象之后获取具体的Service对象,然后才获取Service中的方法等等。所以我们需要注意的是bindService的方式去绑定服务获取的必定是实现了Binder的对象,所以这是我们必须使用Binder的方式去获取Service的方式而不是直接使用Service的类,这个是Android内部实现所约束的。
方法过程如下:
Intent intent = new Intent(MainActivity.this,BindService.class)-&新建了BindService对象-&新建了MyBinder对象
-&bindService(intent, conn, Context.BIND_AUTO_CREATE);-&onBind()函数
-----传递MyBinder对象-------&onServiceConnected()
--& 通过传递的Binder对象获取刚刚和Binder对象对应的BindService 对象  --&调用Service中定义的方法。
这个其中必须通过Binder对象,因为是通过Binder对象来传递的,通过Binder对象获取Service对象,然后获取所需的服务,所以Service必须实现Binder,以便传递和使用。
------------------------------------------
奇怪的事情真多,我的service怎么就有onStart方法,难得是版本的原因了?
package com.endual.
import android.app.S
import android.content.I
import android.os.IB
public class Myservice extends Service{
public IBinder onBind(Intent intent) {
System.out.println("----------&&onBind");
public void onCreate() {
System.out.println("----------&&onCreate");
super.onCreate();
public void onDestroy() {
System.out.println("----------&&onDestroy");
super.onDestroy();
public void onRebind(Intent intent) {
System.out.println("----------&&onRebind");
super.onRebind(intent);
public void onStart(Intent intent, int startId) {
System.out.println("----------&&onStart");
super.onStart(intent, startId);
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("----------&&onStartCommand");
return super.onStartCommand(intent, flags, startId);
public boolean onUnbind(Intent intent) {
System.out.println("----------&&onUnbind");
return super.onUnbind(intent);
浏览: 815070 次
来自: 杭州
spring data jpa相关代码demo下载:http: ...
现在的技术很好的实现这个要求了,比如云适配(www.yunsh ...
Good process
1. Get up-to-date
赞~这个很不错~共勉~Android中怎么启动关闭Service及功能解释_百度知道
Android中怎么启动关闭Service及功能解释
提问者采纳
1.在FireChat中,用户无需登录或者创建帐号,起个称呼就可以进行聊天。2.聊天界面有两个群组可选:Everyone和Nearby。顾名思义,一个是和网络上任意人群聊天的,而另一个则是和附近的人聊天。3.选择了Everyone群组的话,可以看到来自全国各地同时在线的朋友聊天记录,但选择Everyone,是需旦功测嘉爻黄诧萎超联要连接网络才能聊天的。4.Nearby则需要附近有人使用FireChat才能免流量聊天。
其他类似问题
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 android service 启动 的文章

 

随机推荐