android绑定服务方法使用
2015-12-09 20:51
495 查看
1、编写activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开启服务" /> <Button android:onClick="stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止服务" /> <Button android:onClick="bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定服务" /> <Button android:onClick="unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定服务" /> <Button android:onClick="change" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用方法,切换至下一首歌曲" /> </LinearLayout>
<span style="color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 19.5px;">编写MainActivity.java</span>
package com.hyzhou.testservice;import com.hyzhou.testservice.TestServer.MyBinder;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;public class MainActivity extends Activity {//步骤4:在activity里面得到服务IBinder对象的引用private TestServer.MyBinder myBinder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void start(View view){Intent intent=new Intent(this,TestServer.class);startService(intent);}public void stop(View view){Intent intent=new Intent(this,TestServer.class);stopService(intent);}public void bind(View view){Intent intent=new Intent(this,TestServer.class);/*** intent 激活服务意图* conn 代理人中间人对象,用来跟服务建立联系不能为空* BIND_AUTO_CREATE 在绑定服务的时候 如果服务不存在就自动创建*///步骤1:采用绑定的方式开启服务bindService(intent, new MyConn(), BIND_AUTO_CREATE);}public void unbind(View view){Intent intent=new Intent(this,TestServer.class);}public void change(View view){/*** 由于系统框架在创建服务的时候会创建与之对应的上下文* 下面的代码是直接new对象* TestService service=new TestService();* service.changeSing("月亮之上");*///步骤5:利用IBinder对象间接调用服务里面的方法myBinder.callchangeSing("月亮至上");}private class MyConn implements ServiceConnection{//在服务被成功绑定的时候调用的方法@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubSystem.out.println("testserver代理人返回回来了");//步骤3:服务返回的IBinder对象会被传递给MyConn的回调方法myBinder=(MyBinder)service;}//在服务失去绑定的时候调用的方法 只有程序异常终止才会调用该方法@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}}3、编写TestServer服务
package com.hyzhou.testservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;/****//*** @author hyzhou** 2013-12-10*/public class TestServer extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("服务被成功的绑定了------...");//步骤2:服务在成功绑定的时候会调用onBind方法,返回一个IBinder对象return new MyBinder();}public class MyBinder extends Binder{@SuppressWarnings("unused")public void callchangeSing(String name){changeSing(name);}}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("开启服务------...");}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("销毁服务------...");}public void changeSing(String name){Toast.makeText(getApplicationContext(), "切换至当前的音乐"+name, 0).show();}}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories