服务(服务的生命周期&绑定方式开启服务调用服务里的方法&通过接口暴露要对外调用的方法)
package:com.ngu.testservice 目录 testservice src |--com.ngu.testservice |--MainActivity.java |--com.ngu.testservice.service |--MyService.java |--IService.java(接口) res |--layout |--activity_main.xml |--AndroidManifest.xml
MainActivity.java package com.ngu.testservice; 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.Menu; import android.view.View; import com.ngu.testservice.service.IService; import com.ngu.testservice.service.MyService; public class MainActivity extends Activity { private Intent serviceIntent; private MyConn conn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this, MyService.class); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } // start的方式开启服务.调用者退出了,服务还在后台运行,也就是说服务和调用者的生命周期没有关系 public void start(View view) { startService(serviceIntent); System.out.println("startservice"); } // bind的方式开启服务,调用者退出,服务也跟着结束,服务的生命周期和调用者同步 public void bind(View view) { conn = new MyConn(); bindService(serviceIntent, conn, Context.BIND_AUTO_CREATE); System.out.println("bindservice"); } // 停止服务 public void stop(View view) { stopService(serviceIntent); System.out.println("stopservice"); } // 解除绑定服务 public void unbind(View view) { if(conn!=null){ unbindService(conn); conn = null; } System.out.println("unbind"); } @Override protected void onDestroy() { if(conn!=null){ unbindService(conn); conn = null; } super.onDestroy(); } private class MyConn implements ServiceConnection { //调用者中服务被成功绑定的时候调用的方法,对应的在服务中,服务被成功绑定的时候调用:onBind()方法 @Override public void onServiceConnected(ComponentName name, IBinder service) { //MyBinder myBinder = (MyBinder) service; //myBinder.invokeMethod(); IService iService = (IService) service; iService.invokeMethod(); } @Override public void onServiceDisconnected(ComponentName name) { } } }
MyService.java /****************************************** * Copyright (C) 2013 yl * * Good Good Study, Day Day Up. 2013-6-20 ******************************************/ package com.ngu.testservice.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { // 服务第一次被创建的时候调用的方法 @Override public void onCreate() { System.out.println("服务被创建了:onCreate"); super.onCreate(); } // 服务被成功绑定的时候调用的方法 @Override public IBinder onBind(Intent intent) { System.out.println("服务被绑定了:onBind"); return new MyBinder(); } // public class MyBinder extends Binder{ // public void invokeMethod(){ // innerCalledMethod(); // } // } //对于需要暴露出去的方法,一般而言通过接口的方式把方法抽取出来 public class MyBinder extends Binder implements IService{ @Override public void invokeMethod() { innerCalledMethod(); } } // 服务被开启的时候调用的方法 @Override public void onStart(Intent intent, int startId) { System.out.println("服务被开启了:onStart"); super.onStart(intent, startId); } // 服务被销毁的时候调用的方法 @Override public void onDestroy() { System.out.println("服务被销毁了:onDestory"); super.onDestroy(); } // 服务解除绑定的时候调用的方法 @Override public boolean onUnbind(Intent intent) { System.out.println("服务解除绑定:onUnbind"); return super.onUnbind(intent); } /** * 服务中被调用的方法 * @date 2013-6-21 */ public void innerCalledMethod(){ System.out.println("invoke success"); } }
IService.java /****************************************** * Copyright (C) 2013 yl * * Good Good Study, Day Day Up. 2013-6-21 ******************************************/ package com.ngu.testservice.service; public interface IService { public void invokeMethod(); }
activity_main.xml <RelativeLayout 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" tools:context=".MainActivity" > <Button android:id="@+id/bt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:onClick="start" android:text="start" /> <Button android:id="@+id/bt_bind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/bt_start" android:onClick="bind" android:text="bind" /> <Button android:id="@+id/bt_stop_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/bt_bind" android:onClick="stop" android:text="stop service" /> <Button android:id="@+id/bt_unbind" android:onClick="unbind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/bt_stop_service" android:text="unbind" /> </RelativeLayout>
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ngu.testservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.ngu.testservice.MainActivity" 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="com.ngu.testservice.service.MyService" > </service> </application> </manifest>