📄 localservicebinding.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.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * <p>Example of binding and unbinding to the {@link LocalService}. * 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/Local Service Binding <h4>Source files</h4><table class="LinkTable"> <tr> <td class="LinkColumn">src/com/google/android/samples/app/LocalServiceBinding.java</td> <td class="DescrColumn">The Local Service Binding implementation</td> </tr> <tr> <td class="LinkColumn">src/com/google/android/samples/app/LocalService.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 LocalServiceBinding extends Activity{ private boolean mIsBound; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.local_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); } 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. NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notifyWithText(R.string.local_service_connected, getText(R.string.local_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. // Because it is running in our same process, we should never // see this happen. NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notifyWithText(R.string.local_service_disconnected, getText(R.string.local_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(LocalServiceBinding.this, LocalService.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); mIsBound = false; } } };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -