android本地调用service方法
2016-09-18 20:50
387 查看
作为android四大组件之一的service经常被用做后台任务,比如后台播放音乐。但是经常有在页面中调用service中方法的需求。比如后台播放音乐,在前台暂停,开始,快进等操作,需要在activity中调用本地service中的方法
先来说一下service的启动方式。service的启动方式有以下几种
用startService()启动本地服务。
用bindService()启动服务。
混合方式启动本地服务。
这三种方式启动service有什么区别呢?
startService()启动服务无法拿到service对象,就无法调用service中的方法。
bindService()启动服务可以通过ServiceConnection去操作服务中的方法,但是这样启动的服务当启动它的activity关闭时,service会跟着关闭。
混合方式是先用startService()方式启动服务然后用bindService()绑定服务可以访问服务中的方法,activity退出时service不会关闭。
下面以混合方式为例实现该功能
代码如下
布局文件
activity
service
onBind()方式绑定只需要将上面的startService(intent);删掉就行了。
先来说一下service的启动方式。service的启动方式有以下几种
用startService()启动本地服务。
用bindService()启动服务。
混合方式启动本地服务。
这三种方式启动service有什么区别呢?
startService()启动服务无法拿到service对象,就无法调用service中的方法。
bindService()启动服务可以通过ServiceConnection去操作服务中的方法,但是这样启动的服务当启动它的activity关闭时,service会跟着关闭。
混合方式是先用startService()方式启动服务然后用bindService()绑定服务可以访问服务中的方法,activity退出时service不会关闭。
下面以混合方式为例实现该功能
代码如下
布局文件
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zl.itgeek.servicedemo.MainActivity" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="a" android:onClick="a"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="b" android:onClick="b"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="c" android:onClick="c"/> </LinearLayout>
activity
import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { MyService.MyBinder myBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this,MyService.class); startService(intent); bindService(intent,new Connect(),0); } public void a(View v){ myBinder.toA(); } public void b(View v){ myBinder.toB(); } public void c(View v){ myBinder.toC(); } class Connect implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { myBender = (MyService.MyBinder) service; } @Override public void onServiceDisconnected(ComponentName name) { } } }
service
package com.zl.itgeek.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); Toast.makeText(this,"服务启动了",Toast.LENGTH_SHORT); } @Override public IBinder onBind(Intent intent) { return new MyBinder(); } class MyBinder extends Binder{ public void toA(){ a(); }public void toB(){ b(); }public void toC(){ c(); } } public void a(){ Toast.makeText(this,"a",Toast.LENGTH_SHORT); Log.e("com.deubg.zl","a方法执行成功了"); } public void b(){ Toast.makeText(this,"b",Toast.LENGTH_SHORT); Log.e("com.deubg.zl","b方法执行成功了"); } public void c(){ Toast.makeText(this,"c",Toast.LENGTH_SHORT); Log.e("com.deubg.zl","c方法执行成功了"); } }
onBind()方式绑定只需要将上面的startService(intent);删掉就行了。
相关文章推荐
- android基础笔记:服务(Service) 采用接口的方式调用本地服务的方法
- android 中activity调用本地service中的方法。
- Android 本地Activity与Service通信方法
- Android不同系统版本依然能调用到正确的API方法Demo——Service调用startForeground举例
- [转]Android通过NDK调用JNI,使用opencv做本地c++代码开发配置方法
- Android的音频解码原来是直接调用的本地C方法直接通过硬件解码
- 调用android非unbind的服务中的方法(不使用bindService启动的服务)
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- Android AIDL bindService后不能调用onServiceConnected方法(一种情况)
- Android service的开启和绑定,以及调用service的方法
- 编写android程序调用jni本地方法的实现(详细例子)
- Android不同系统版本依然能调用到正确的API方法Demo——Service调用startForeground举例
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- android webview 调用本地java代码失败解决方法
- Android 调用Remote Service方法
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- 在Activity中调用本地服务(Service)中的方法
- Android -- service 利用广播调用服务的方法
- 在android中通过JNI调用本地方法