BindService总结
一、整体工程图
二、activity_bind.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <Button android:text="useService"
android:id="@+id/buttonUseService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>
三、BindActivity.java
package com.jltxgcy.bindservice; import android.app.Activity;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast; import com.jltxgcy.bindservice.LocalService.LocalBinder; public class BindActivity extends Activity { LocalService mService;
boolean mBound = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind);
findViewById(R.id.buttonUseService).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
int num = mService.getRandomNumber();
Toast.makeText(BindActivity.this, "number: " + num, Toast.LENGTH_SHORT).show();
} }
});
} @Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
} @Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
} /** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() { @Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
Log.d("jltxgcy", "onServiceConnected");
} //Called when the connection with the service disconnects unexpectedly
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
Log.d("jltxgcy", "onServiceDisconnected");
}
};
}
四、LocalService.java
package com.jltxgcy.bindservice; import java.util.Random; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class LocalService extends Service {
public static final String TAG="jltxgcy";
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random(); @Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
} /**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
} @Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return mBinder;
} /** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
} @Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind");
return super.onUnbind(intent);
} @Override
public void onDestroy() {
Log.d(TAG, "onDestroy"); }
}
五、详解
程序开始,绑定Service,结果显示如下:
说明了调用的先后顺序。onBind方法得到了IBinder对象,onServiceConnected方法将IBinder对象向下转型为LocalBinder对象,通过调用getService取得Service实例, 这样就可以在BindActivity中使用Service的方法。
按Back键退出,调用
BindService总结的更多相关文章
- Android之startService()和bindService()区别
1. 生命周期: startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy(), ...
- 实验7 BindService模拟通信
实验报告 课程名称 基于Android平台移动互联网开发 实验日期 2016.4.16 实验项目名称 BindService模拟通信 实验地点 S30010 实验类型 □验证型 √设计型 ...
- bindService初步了解
bindService的使用: 当需要调Service里面的方法时,可以用bindService() 首先定义一个类继承于Service,然后配置Manifest.xml文件 public class ...
- [Android Pro] Service (startservice , bindservice , unbindservice, stopService)
1: startService -------stopService (this will call onDestroy) 2: bindService -------unbindService ...
- [转]安卓开发startservice 和bindservice详解
原文 作者:aikongmeng 来源:安卓中文网 博主暗表:搜到此文,终于为我解惑,bindService并不会真正启动service,不会调用onStartCommand!还需要再bind之前st ...
- 深入理解Android的startservice和bindservice
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...
- Android bindservice使用
package com.example.myact10; import com.example.myact10.MyService.MyBinder; import android.support.v ...
- startService()和bindService()区别
1. 生命周期:startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy(),而 ...
- 【Android 界面效果34】Android里Service的bindService()和startService()混合使用深入分析
.先讲讲怎么使用bindService()绑定服务 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回 ...
- bindService和startService的区别
区别: startService,关闭服务退出activity,service仍然处于后台运行 bindService,关闭服务退出activity直接stopService,停止服务 bindSer ...
随机推荐
- 第34讲 UI组件之 ProgressDialog和Message
第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...
- CoreText中坐标转换的一些理解
引言 学习CoreText,最初的想法是写一个杂志类的应用,因为对网易和zarca应用一些技术的疑问,所以,自己有了很强的兴趣欲和钻研欲,开始这段有点不顺的学习过程. 难题 1.对CGContextR ...
- .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法
1.添加引用 using Oracle.DataAccess.Client; using System.Configuration; 2.代码 增加方法 //DestinationTableName ...
- IOS网络开发实战(一)
1 局域网群聊软件 1.1 问题 UDP协议将独立的数据包从一台计算机传输到另外一台计算机,但是并不保证接受方能够接收到该数据包,也不保证接收方所接收到的数据和发送方所发送的数据在内容和顺序上是完 ...
- OD调试9—实例:深入分析代码完成软件破解
OD调试9—实例:深入分析代码完成软件破解 爆破,是最初级的解决方案,不到万不得已,我们不直接修改JNZ通关.因为这样子的话,我们就享受不到破解.逆向的真正乐趣了. 了解程序背后按照剧情发展经常会出 ...
- JVM的内存区域划分划分及作用
- C#将十六进制的文本转换到整型数据
1 length1 = Int32.Parse(szLine.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);//计算这一行 ...
- OpenSuse13.2安装CUDA Toolkit 7.5
此次安装过程有点曲折,不过最后还是能成功安装,由于没细细看官方的安装文档,导致花了很多时间安装,希望此文能让想装CUDA的同学少走点弯路 1.NVIDIA Driver是否要装 刚开始要装CUDA时, ...