我有一壶酒 Android学习之Service(1)--->BinderService方式
本文只讨论扩展Binder类
创建一个Binder.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btnStartBinderService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start BinderService"
/>
<Button
android:id="@+id/btnStopBinderService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop BinderService"
/>
</LinearLayout>
创建一个BinderService.jvaa类,继承Service
package com.szy.service; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class BinderService extends Service
{
private static final String TAG = "BinderService";
private MyBinder binder =new MyBinder(); public class MyBinder extends Binder
{
public BinderService getService()
{
return BinderService.this;
}
}
@Override
public IBinder onBind(Intent intent)
{
return binder;
} public void MyMethod()
{
Log.i(TAG, "MyMethod()");
} }
再新建一个类BinderActivity.java继承Activity
package com.szy.service; import com.szy.service.BinderService.MyBinder; 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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class BinderActivity extends Activity
{
private Button btnStartBinderService;
private Button btnStopBinderService; private Boolean isConnected = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.binder);
btnStartBinderService=(Button)findViewById(R.id.btnStartBinderService);
btnStopBinderService=(Button)findViewById(R.id.btnStopBinderService);
btnStartBinderService.setOnClickListener(listener);
btnStopBinderService.setOnClickListener(listener);
} private OnClickListener listener=new OnClickListener()
{ public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnStartBinderService:
bindService();
break;
case R.id.btnStopBinderService:
unBind();
break;
default:
break;
}
} }; private void unBind()
{
if (isConnected)
{
unbindService(conn);
}
} private void bindService()
{
Intent intent=new Intent(BinderActivity.this, BinderService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} private ServiceConnection conn=new ServiceConnection()
{ public void onServiceDisconnected(ComponentName name)
{
isConnected=false;
} public void onServiceConnected(ComponentName name, IBinder binder)
{
MyBinder myBinder= (MyBinder)binder;
BinderService service=myBinder.getService();
service.MyMethod();
isConnected=true; }
};
}
修改下AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.szy.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
</activity> <activity android:name=".BinderActivity"
android:label="@string/app_name">
</activity> <activity android:name=".IntentActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name=".ExampleService" />
<service android:name=".BinderService" />
<service android:name=".MyService"/>
<service android:name=".ExampleIntentService"/>
</application>
</manifest>
我有一壶酒 Android学习之Service(1)--->BinderService方式的更多相关文章
- Android学习之Service(1)--->Started方式
界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" enco ...
- Android学习总结——Service组件
从Service的启动方式上,可以将Service分为Started Service和Bound Service.在使用Service时,要想系统能够找到此自定义Service,无论哪种类型,都需要在 ...
- Android 学习笔记 Service服务与远程通信...(AIDL)
PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...
- Android 学习笔记 Service
PS:前几篇的内容光是上代码了,也没有细细的讲解..感觉这样写很不好..因此还是多一些讲解吧... 学习内容: 1.了解Service... 2.Service的启动与停止.. 3.绑定与取消绑定Se ...
- 【Android学习】Service&Boradcast初步
Service初步 掌握Service概念 掌握Service分类 Service开发能力具备 了解Service和intentService类的区别 重点难点 StartService和BoundS ...
- 【Android学习】四种布局方式
一.LinearLayout 线性布局,即一行展开或者一列展开,也可以嵌套,需要注意的属性如下: android:orentation //对齐方式 二.FrameLayout 帧布局,即一层层叠起 ...
- android学习笔记 Service
Service(服务): 长期后台运行的没有界面的组件 android应用什么地方需要用到服务? 天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息.股票显示:后台的连接服务器的逻辑,每 ...
- Android学习笔记④——页面的布局方式
FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...
- Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)
转自:http://blog.csdn.net/wulianghuan/article/details/8626551 我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的 ...
随机推荐
- Chapter 1 First Sight——32
The class seemed to drag on longer than the others. 这堂课看起来比别的课要长. Was it because the day was finally ...
- php mysql数据库 分页与搜索
<?php/** * Created by coder meng. * User: coder meng * Date: 2016/8/29 10:27 */header("Conte ...
- Broken Keyboard(悲剧文本)
你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下.你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源.当你打开显示器之后,展现在你面前的是一段悲剧文本.你的任 ...
- 缩放系列(三):一个可以手势缩放、拖拽、旋转的layout
弄了一个下午,终于搞出来了,PowerfulLayout 下面是一个功能强大的改造的例子: 可以实现以下需求: 1.两个手指进行缩放布局 2.所有子控件也随着缩放, 3.子控件该有的功能不能丢失(像b ...
- 去掉input text后面的叉
如题 input[type=text]::-ms-clear{ display: none; } input::-webkit-search-cancel-button{ display: none; ...
- Bitmap和Drawable浅谈
一.概念区别 Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB8888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一 ...
- ACC起来后,usb检测不到
/proc/scsi/usb-storage 插入u盘,生成文件 /dev/sd* 节点路径 挂载方法 mkdir /media/sda1 mount /dev/sda1 /media/sda1 u盘 ...
- perl中调用cgi
来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698856.html 参考:http://www.willmaster.com/librar ...
- wl18xx module crash with "wlcore: ERROR ELP wakeup timeout!"
[ 111.322967] wlcore: ERROR ELP wakeup timeout![ 111.327636] ------------[ cut here ]------------[ 1 ...
- LINQ&EF in 用算的写法 like