016_02startService与bindService的比较
使用startService()方法启动service,调用者与service之间没有关联,即使调用者退出了,服务仍然运行。
使用bindService()方法启动service,调用者与service绑定在一起,调用者一旦退出,服务也就终止。
Service使用了onBind 的方法去绑定服务,并返回一个Ibinder对象。具体的Service对象首先需要实现Binder对象,然后利用bindService的方法对Service进行绑定。当Ibinder对象返回具体的Service对象后,才可以获取service方法的内容。
com.example.day16_02servicedemo.MainActivity
1 public void bindservice(View v){ 2 Intent service = new Intent(this, MyService.class); 3 conn =new MyServiceConnection(); 4 bindService(service, conn, Context.BIND_AUTO_CREATE); 5 }
1 class MyServiceConnection implements ServiceConnection{ 2 @Override 3 public void onServiceConnected(ComponentName name, IBinder service) { 4 binder = (MyBinder) service; 5 System.out.println("MainActivity.MyServiceConnection.onServiceConnected()"); 6 } 7 8 @Override 9 public void onServiceDisconnected(ComponentName name) { 10 System.out.println("MainActivity.MyServiceConnection.onServiceDisconnected()"); 11 } 12 }
com.example.day16_02servicedemo.MyService
1 public IBinder onBind(Intent intent) { 2 // TODO Auto-generated method stub 3 System.out.println("MyService.onBind()"); 4 //当且仅当此处返回一个非空的Ibinder对象时,bindservice一端的Connection内的onserviceConnected函数才会被系统call到 5 return new MyBinder(); 6 }
1 class MyBinder extends Binder{ 2 //内部类里面可以直接调用外部类的方法 3 public String forworadgetruntimeinfo(){ 4 return getruntimeinfo(); 5 } 6 }
1,当点击“启动服务”,Logcat输出:
2,退出该APP后,服务仍然存在;当重新回来点击“停止服务”,Logcat输出:
3,点击“绑定服务”,Logcat输出:
4,点击“解绑服务”或者退出App,Logcat输出:
相关源码如下:
1 package com.example.day16_02servicedemo; 2 3 import com.example.day16_02servicedemo.MyService.MyBinder; 4 import android.app.Activity; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.ServiceConnection; 9 import android.os.Bundle; 10 import android.os.IBinder; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 MyServiceConnection conn; 15 MyBinder binder; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 } 21 22 public void startservice(View v){ 23 Intent service = new Intent(this, MyService.class); 24 startService(service); 25 } 26 27 public void stopservice(View v){ 28 Intent service = new Intent(this, MyService.class); 29 stopService(service); 30 } 31 32 public void bindservice(View v){ 33 Intent service = new Intent(this, MyService.class); 34 conn =new MyServiceConnection(); 35 bindService(service, conn, Context.BIND_AUTO_CREATE); 36 } 37 38 public void unbindservice(View v){ 39 unbindService(conn); 40 conn =null; 41 } 42 43 44 @Override 45 protected void onDestroy() { 46 // TODO Auto-generated method stub 47 super.onDestroy(); 48 if (conn!=null) { 49 unbindService(conn); 50 } 51 } 52 53 class MyServiceConnection implements ServiceConnection{ 54 @Override 55 public void onServiceConnected(ComponentName name, IBinder service) { 56 binder = (MyBinder) service; 57 System.out.println("MainActivity.MyServiceConnection.onServiceConnected()"); 58 } 59 60 @Override 61 public void onServiceDisconnected(ComponentName name) { 62 System.out.println("MainActivity.MyServiceConnection.onServiceDisconnected()"); 63 } 64 } 65 66 public void callservicefunc(View v){ 67 //调用service内的方法 68 //直接new service对象这种方式不行 69 //需要从service给你传回来的这个内部类着手,解决问题 70 String info= binder.forworadgetruntimeinfo(); 71 System.out.println("MainActivity.callservicefunc()"+info); 72 } 73 }
1 package com.example.day16_02servicedemo; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyService extends Service { 9 @Override 10 public IBinder onBind(Intent intent) { 11 // TODO Auto-generated method stub 12 System.out.println("MyService.onBind()"); 13 //当且仅当此处返回一个非空的Ibinder对象时,bindservice一端的Connection内的onserviceConnected函数才会被系统call到 14 return new MyBinder(); 15 } 16 17 public String getruntimeinfo(){ 18 String info = "momory usage 5%"; 19 return info; 20 } 21 22 @Override 23 public boolean onUnbind(Intent intent) { 24 // TODO Auto-generated method stub 25 System.out.println("MyService.onUnbind()"); 26 return super.onUnbind(intent); 27 } 28 29 @Override 30 public void onRebind(Intent intent) { 31 // TODO Auto-generated method stub 32 super.onRebind(intent); 33 System.out.println("MyService.onRebind()"); 34 } 35 36 @Override 37 public void onCreate() { 38 // TODO Auto-generated method stub 39 super.onCreate(); 40 System.out.println("MyService.onCreate()"); 41 } 42 43 @Override 44 public int onStartCommand(Intent intent, int flags, int startId) { 45 // TODO Auto-generated method stub 46 System.out.println("MyService.onStartCommand()"); 47 return super.onStartCommand(intent, flags, startId); 48 } 49 50 @Override 51 public void onDestroy() { 52 // TODO Auto-generated method stub 53 super.onDestroy(); 54 System.out.println("MyService.onDestroy()"); 55 } 56 57 class MyBinder extends Binder{ 58 //内部类里面可以直接调用外部类的方法 59 public String forworadgetruntimeinfo(){ 60 return getruntimeinfo(); 61 } 62 } 63 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.day16_02servicedemo" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <service android:name=".MyService"> 26 <intent-filter> 27 <action android:name="com.cskaoyan.service" /> 28 </intent-filter> 29 </service> 30 </application> 31 32 </manifest>
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.day16_04remoteservicedemo.MainActivity" 10 android:orientation="vertical"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 <Button 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="启动服务" 20 android:onClick="startservice"/> 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="停止服务" 25 android:onClick="stopservice"/> 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="绑定服务" 30 android:onClick="bindservice"/> 31 <Button 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="解绑服务" 35 android:onClick="unbindservice"/> 36 <Button 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text="获取memory usage" 40 android:onClick="callservicefunc"/> 41 </LinearLayout>
物随心转,境由心造,一切烦恼皆由心生。