⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remoteservicebinding.java

📁 android 模拟器
💻 JAVA
字号:
/*  * Copyright (C) 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.android.samples.app;import com.google.android.samples.R;import android.app.Activity;import android.app.NotificationManager;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.DeadObjectException;import android.os.IBinder;import android.os.Process;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * <p>Example of binding and unbinding to the {@link RemoteService}. * This demonstrates the implementation of a service which the client willbind to, receiving an object through which it can communicate with the service.</p><h4>Demo</h4>App/Service/Remote Service Binding <h4>Source files</h4><table class="LinkTable">        <tr>            <td class="LinkColumn">src/com/google/android/samples/app/RemoteServiceBinding.java</td>            <td class="DescrColumn">The Remote Service Binding implementation</td>        </tr>        <tr>            <td class="LinkColumn">src/com/google/android/samples/app/RemoteService.java</td>            <td class="DescrColumn">The implementation of the service itself</td>        </tr>        <tr>            <td class="LinkColumn">/res/any/layout/local_service_binding.xml</td>            <td class="DescrColumn">Defines contents of the screen</td>        </tr></table> */public class RemoteServiceBinding extends Activity{    IRemoteService mService = null;    Button mKillButton;    private boolean mIsBound;    @Override    protected void onCreate(Bundle icicle)    {        super.onCreate(icicle);        setContentView(R.layout.remote_service_binding);        // Watch for button clicks.        Button button = (Button)findViewById(R.id.bind);        button.setOnClickListener(mBindListener);        button = (Button)findViewById(R.id.unbind);        button.setOnClickListener(mUnbindListener);        mKillButton = (Button)findViewById(R.id.kill);        mKillButton.setOnClickListener(mKillListener);        mKillButton.setEnabled(false);    }    private ServiceConnection mConnection = new ServiceConnection()    {        public void onServiceConnected(ComponentName className, IBinder service)        {            // This is called when the connection with the service has been            // established, giving us the service object we can use to            // interact with the service.  We are communicating with our            // service through an IDL interface, so get a client-side            // representation of that from the raw service object.            mService = IRemoteService.Stub.asInterface((IBinder)service);            mKillButton.setEnabled(true);            // As part of the sample, tell the user what happened.            NotificationManager nm = (NotificationManager)                getSystemService(NOTIFICATION_SERVICE);            nm.notifyWithText(R.string.remote_service_connected,                      getText(R.string.remote_service_connected),                      NotificationManager.LENGTH_SHORT,                      null);        }        public void onServiceDisconnected(ComponentName className)        {            // This is called when the connection with the service has been            // unexpectedly disconnected -- that is, its process crashed.            mService = null;            mKillButton.setEnabled(false);            // As part of the sample, tell the user what happened.            NotificationManager nm = (NotificationManager)                    getSystemService(NOTIFICATION_SERVICE);            nm.notifyWithText(R.string.remote_service_disconnected,                      getText(R.string.remote_service_disconnected),                      NotificationManager.LENGTH_SHORT,                      null);        }    };    private OnClickListener mBindListener = new OnClickListener()    {        public void onClick(View v)        {            // Establish a connection with the service, by its class name.            bindService(new Intent(RemoteServiceBinding.this,                        RemoteService.class),                    null, mConnection, Context.BIND_AUTO_CREATE);            mIsBound = true;        }    };    private OnClickListener mUnbindListener = new OnClickListener()    {        public void onClick(View v)        {            if (mIsBound) {                // Detach our existing connection.                unbindService(mConnection);                mKillButton.setEnabled(false);                mIsBound = false;            }        }    };    private OnClickListener mKillListener = new OnClickListener()    {        public void onClick(View v)        {            // To kill the process hosting our service, we need to know its            // PID.  Conveniently our service has a call that will return            // to us that information.            if (mService != null) {                int pid = -1;                try {                    pid = mService.getPid();                } catch (DeadObjectException ex) {                    // Recover gracefully from the process hosting the                    // server dying.                    // Just for purposes of the sample, put up a notification.                    NotificationManager nm = (NotificationManager)                        getSystemService(NOTIFICATION_SERVICE);                    nm.notifyWithText(R.string.remote_call_failed,                              getText(R.string.remote_call_failed),                              NotificationManager.LENGTH_SHORT,                              null);                }                if (pid > 0) {                    // Go away!                    Process.killProcess(pid);                }            }        }    };}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -