bind/unbind服务示例(android)

use*_*280 53 service binding android

你能给我一个简单的后台服务应用程序示例,它使用bind/unbind方法来启动和停止它吗?我正在谷歌搜索半个小时,但这些示例使用startService/stopService方法或对我来说非常困难.谢谢.

Daw*_*dak 61

您可以尝试使用此代码:

protected ServiceConnection mServerConn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        Log.d(LOG_TAG, "onServiceConnected");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(LOG_TAG, "onServiceDisconnected");
    }
}

public void start() {
    // mContext is defined upper in code, I think it is not necessary to explain what is it 
    mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
    mContext.startService(intent);
}

public void stop() {
    mContext.stopService(new Intent(mContext, ServiceRemote.class));
    mContext.unbindService(mServerConn);
}
Run Code Online (Sandbox Code Playgroud)

  • 请告诉我,什么是varialbe我? (6认同)

HiB*_*HiB 42

将这些方法添加到您的Activity中:

private MyService myServiceBinder;
public ServiceConnection myConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder binder) {
        myServiceBinder = ((MyService.MyBinder) binder).getService();
        Log.d("ServiceConnection","connected");
        showServiceData();
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.d("ServiceConnection","disconnected");
        myService = null;
    }
};

public Handler myHandler = new Handler() {
    public void handleMessage(Message message) {
        Bundle data = message.getData();
    }
};

public void doBindService() {
    Intent intent = null;
    intent = new Intent(this, BTService.class);
    // Create a new Messenger for the communication back
    // From the Service to the Activity
    Messenger messenger = new Messenger(myHandler);
    intent.putExtra("MESSENGER", messenger);

    bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)

您可以通过ovverriding onResume()和Activity类的onPause()来绑定服务.

@Override
protected void onResume() {

    Log.d("activity", "onResume");
    if (myService == null) {
        doBindService();
    }
    super.onResume();
}

@Override
protected void onPause() {
    //FIXME put back

    Log.d("activity", "onPause");
    if (myService != null) {
        unbindService(myConnection);
        myService = null;
    }
    super.onPause();
}
Run Code Online (Sandbox Code Playgroud)

请注意,绑定到服务时,只onCreate()在服务类中调用该方法.在Service类中,您需要定义myBinder方法:

private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;

@Override
public IBinder onBind(Intent arg0) {
    Bundle extras = arg0.getExtras();
    Log.d("service","onBind");
    // Get messager from the Activity
    if (extras != null) {
        Log.d("service","onBind with extra");
        outMessenger = (Messenger) extras.get("MESSENGER");
    }
    return mBinder;
}

public class MyBinder extends Binder {
    MyService getService() {
        return MyService.this;
    }
}
Run Code Online (Sandbox Code Playgroud)

定义了这些方法后,您可以在Activity中找到服务的方法:

private void showServiceData() {  
    myServiceBinder.myMethod();
}
Run Code Online (Sandbox Code Playgroud)

最后你可以在发生某些事件时启动你的服务,如_BOOT_COMPLETED_

public class MyReciever  extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("android.intent.action.BOOT_COMPLETED")) {
            Intent service = new Intent(context, myService.class);
            context.startService(service);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意,当启动一个服务onCreate()onStartCommand()被称为服务类,当另一个事件发生时由您可以停止你的服务stopService() 注意,您的事件侦听器应该在你的Android清单文件中registerd:

<receiver android:name="MyReciever" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)


Har*_*era 15

首先,我们需要了解的两件事,

客户

  • 它向特定服务器发出请求

bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);

这里mServiceConnServiceConnection类(内置)的实例它实际上是我们需要用两个(第一个用于网络连接和第二个网络未连接)方法来实现的接口,用于监视网络连接状态.

服务器

  • 它处理客户端的请求并制作自己的副本,这是我们发送请求的客户端专用的,并且这个服务器的raplica在不同的线程上运行.

现在在客户端,如何访问服务器的所有方法?

  • 服务器使用IBinderObject 发送响应.因此,IBinderobject是我们的处理程序,它Service使用(.)运算符访问所有方法.

.

MyService myService;
public ServiceConnection myConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        Log.d("ServiceConnection","connected");
        myService = binder;
    }
    //binder comes from server to communicate with method's of 

    public void onServiceDisconnected(ComponentName className) {
        Log.d("ServiceConnection","disconnected");
        myService = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在如何调用服务中的方法

myservice.serviceMethod();
Run Code Online (Sandbox Code Playgroud)

myService是object和serviceMethod是服务中的方法.通过这种方式,在客户端和服务器之间建立通信.