Android service的开启和绑定,以及调用service的方法
2014-05-17 11:26
232 查看
界面:
Activity
Service
<?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:layout_width="match_parent" android:layout_height="wrap_content" android:text="开启服务" android:onClick="start" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="关闭服务" android:onClick="close" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="绑定服务" android:onClick="bind" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消绑定服务" android:onClick="unbind" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调用服务的方法function()" android:onClick="callFunction" /> </LinearLayout>
Activity
package com.example.serviceTest; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; public class MyActivity extends Activity { private IService myBinder; private ServiceConnection myConn = new MyConn(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void start(View view) { startService(new Intent(this, TestService.class)); } /** * 多次调用停止服务,没有出现问题(不能多次解绑) */ public void close(View view) { stopService(new Intent(this, TestService.class)); } public void bind(View view) { Intent intent = new Intent(this, TestService.class); bindService(intent, myConn, BIND_AUTO_CREATE); } /** * 多次解绑服务会报出错误 */ public void unbind(View view) { unbindService(myConn); } public void callFunction(View view) { if (myBinder != null) { myBinder.callFunction(); } } //绑定的时候,回调的一些方法 class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("获取到binder"); myBinder = (IService) service; } @Override public void onServiceDisconnected(ComponentName name) { System.out.println("com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected"); } } }
Service
package com.example.serviceTest; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; /** * Created by Heyiyong on 14-5-16. */ public class TestService extends Service { public IBinder onBind(Intent intent) { System.out.println("服务被绑定了!"); return new MyBinder(); } //中间人(service的代理) private class MyBinder extends Binder implements IService{ public void callFunction() { function(); } } @Override public void onCreate() { System.out.println("com.example.serviceTest.TestService.onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("com.example.serviceTest.TestService.onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { System.out.println("com.example.serviceTest.TestService.onDestroy"); } @Override public boolean onUnbind(Intent intent) { System.out.println("com.example.serviceTest.TestService.onUnbind"); return super.onUnbind(intent); } public void function() { Toast.makeText(getApplicationContext(), "function()方法被调用了!", 1).show(); System.out.println("com.example.serviceTest.TestService.function"); } }
相关文章推荐
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- android java开发 第三天 之 Activity 与 Service 绑定(activiey 调用service中方法)
- [android] 绑定方式开启服务&调用服务的方法
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- Android中服务里面方法的调用,开启,绑定【入门】
- Android - 绑定服务调用服务里面的方法,start/bind开启服务的比较,混合方式开启服务,本地服务,远程服务,系统服务,录音API
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
- Android 开机震动的调用位置以及打开关闭方法
- Android 绑定远程Service(此Service开启在另外一个进程中)
- android中服务Service,开启服务与绑定服务的区别与联系
- Android不同系统版本依然能调用到正确的API方法Demo——Service调用startForeground举例
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 绑定Service的实现方法二:使用Messenger
- android bindService 来调用服务中的方法
- Android 在Service开启对话框的方法
- Android 绑定Service的实现方法二:使用Messenger
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 绑定Service的实现方法一:扩展Binder类来构建接口