service的用法

来源:互联网 发布:通信网络代维 外包 编辑:程序博客网 时间:2024/05/18 00:58

Service是后台运行,不可见,没有界面的页面,优先级高于Activity,可以用来播放音乐、记录地理信息位置的改变、监听某种动作,类型有两种,一是本地服务,有start和bind两种启动方式,另一种是远程服务。

目标效果:

 

程序运行显示所有的按钮控件,分为两类,上边是start的启动和停止,下边是bind的启动和停止,点击输出对应的生命周期的方法内容。


1.activity_main.xml页面放置所有按钮控件。

activity_main.xml页面:

[html] view plain copy
  1. <RelativeLayout 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.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tvStart"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:text="Start:" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/btStart"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_below="@+id/tvStart"  
  22.         android:text="StartService" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/btStop"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignParentLeft="true"  
  29.         android:layout_alignParentRight="true"  
  30.         android:layout_below="@+id/btStart"  
  31.         android:text="StopService" />  
  32.   
  33.     <TextView  
  34.         android:id="@+id/tvBind"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_alignParentLeft="true"  
  38.         android:layout_below="@+id/btStop"  
  39.         android:text="Bind:" />  
  40.   
  41.     <Button  
  42.         android:id="@+id/btBind"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignParentLeft="true"  
  46.         android:layout_alignParentRight="true"  
  47.         android:layout_below="@+id/tvBind"  
  48.         android:text="BindService" />  
  49.   
  50.     <LinearLayout  
  51.         android:id="@+id/changeOne"  
  52.         android:layout_width="match_parent"  
  53.         android:layout_height="wrap_content"  
  54.         android:layout_below="@+id/btBind"  
  55.         android:orientation="horizontal" >  
  56.   
  57.         <Button  
  58.             android:id="@+id/btPrevious"  
  59.             android:layout_width="wrap_content"  
  60.             android:layout_height="wrap_content"  
  61.             android:layout_alignParentRight="true"  
  62.             android:layout_weight="1"  
  63.             android:text="上一首" />  
  64.   
  65.         <Button  
  66.             android:id="@+id/btNext"  
  67.             android:layout_width="wrap_content"  
  68.             android:layout_height="wrap_content"  
  69.             android:layout_alignParentRight="true"  
  70.             android:layout_weight="1"  
  71.             android:text="下一首" />  
  72.     </LinearLayout>  
  73.   
  74.     <LinearLayout  
  75.         android:id="@+id/changeTwo"  
  76.         android:layout_width="match_parent"  
  77.         android:layout_height="wrap_content"  
  78.         android:layout_below="@+id/changeOne"  
  79.         android:orientation="horizontal" >  
  80.   
  81.         <Button  
  82.             android:id="@+id/btPlay"  
  83.             android:layout_width="wrap_content"  
  84.             android:layout_height="wrap_content"  
  85.             android:layout_alignParentRight="true"  
  86.             android:layout_weight="1"  
  87.             android:text="播放" />  
  88.   
  89.         <Button  
  90.             android:id="@+id/btPause"  
  91.             android:layout_width="wrap_content"  
  92.             android:layout_height="wrap_content"  
  93.             android:layout_alignParentRight="true"  
  94.             android:layout_weight="1"  
  95.             android:text="暂停" />  
  96.     </LinearLayout>  
  97.   
  98.     <Button  
  99.         android:id="@+id/btUnBind"  
  100.         android:layout_width="wrap_content"  
  101.         android:layout_height="wrap_content"  
  102.         android:layout_alignParentLeft="true"  
  103.         android:layout_alignParentRight="true"  
  104.         android:layout_below="@+id/changeTwo"  
  105.         android:text="UnBindService" />  
  106.   
  107. </RelativeLayout>  


2.新建myStartService.java页面继承Service,重写方法start方式启动时调用。
myStartService.java页面:
[java] view plain copy
  1. package com.example.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.util.Log;  
  7.   
  8. public class myStartService extends Service{  
  9.   
  10.     @Override  
  11.     public IBinder onBind(Intent arg0) {  
  12.         Log.i("MainActivity","startService--onBind()");  
  13.         return null;  
  14.     }  
  15.     @Override  
  16.     public void onCreate() {  
  17.         Log.i("MainActivity","startService--onCeate()");  
  18.         super.onCreate();  
  19.     }  
  20.     @Override  
  21.     public int onStartCommand(Intent intent, int flags, int startId) {  
  22.         Log.i("MainActivity","startService--onStartCommand()");  
  23.         return super.onStartCommand(intent, flags, startId);  
  24.     }  
  25.     @Override  
  26.     public void onDestroy() {  
  27.         super.onDestroy();  
  28.         Log.i("MainActivity","startService--onDestroy()");  
  29.     }  
  30. }  


3.新建myBindService.java页面继承Service,重写方法,bind方式启动时调用。
myBindService.java页面:
[java] view plain copy
  1. package com.example.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.content.ServiceConnection;  
  6. import android.os.Binder;  
  7. import android.os.IBinder;  
  8. import android.util.Log;  
  9.   
  10. public class myBindService extends Service{  
  11.   
  12.     /*定义类继承Binder返回服务对象*/  
  13.     public class MyBinder extends Binder{  
  14.         public myBindService getService(){  
  15.             return myBindService.this;  
  16.         }  
  17.     }  
  18.     @Override  
  19.     public IBinder onBind(Intent arg0) {  
  20.         Log.i("MainActivity","bindService--onBind()");  
  21.         return new MyBinder();//实例服务对象并返回  
  22.     }  
  23.     @Override  
  24.     public void onCreate() {  
  25.         super.onCreate();  
  26.         Log.i("MainActivity","bindService--onCeate()");  
  27.     }  
  28.     @Override  
  29.     public void unbindService(ServiceConnection conn) {  
  30.         super.unbindService(conn);  
  31.         Log.i("MainActivity","bindService--unbindService()");  
  32.     }  
  33.     @Override  
  34.     public void onDestroy() {  
  35.         super.onDestroy();  
  36.         Log.i("MainActivity","bindService--onDestroy()");  
  37.     }  
  38.     public void play(){  
  39.         Log.i("MainActivity","播放");  
  40.     }  
  41.     public void pause(){  
  42.         Log.i("MainActivity","暂停");  
  43.     }  
  44.     public void previous(){  
  45.         Log.i("MainActivity","上一首");  
  46.     }  
  47.     public void next(){  
  48.         Log.i("MainActivity","下一首");  
  49.     }  
  50. }  


4.对于新建的两个Service,都需要在AndroidManifest.xml页面进行注册。
AndroidManifest.xml页面:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.service"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="17"  
  9.         android:targetSdkVersion="19" />  
  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="com.example.service.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.           
  26.         <!-- 注册Service -->  
  27.         <service android:name="com.example.service.myStartService"></service>  
  28.         <service android:name="com.example.service.myBindService"></service>  
  29.     </application>  
  30.   
  31. </manifest>  


5.MainActivity.java页面进行点击事件的绑定。
MainActivity.java页面:
[java] view plain copy
  1. package com.example.service;  
  2.   
  3. import com.example.service.myBindService.MyBinder;  
  4.   
  5. import android.os.Bundle;  
  6. import android.os.IBinder;  
  7. import android.app.Activity;  
  8. import android.app.Service;  
  9. import android.content.ComponentName;  
  10. import android.content.Intent;  
  11. import android.content.ServiceConnection;  
  12. import android.view.Menu;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16.   
  17. public class MainActivity extends Activity implements OnClickListener {  
  18.   
  19.     private Button btStart, btStop, btBind, btPrevious, btNext, btPlay,  
  20.             btPause, btUnBind;  
  21.     private Intent intentStart,intentBind;  
  22.     private myBindService service;  
  23.       
  24.     /*得到服务对象*/  
  25.     private ServiceConnection conn=new ServiceConnection() {  
  26.           
  27.         /*当启动源跟Service的连接意外丢失的时候会调用这个方法,比如当Service崩溃了或者被强行杀死了*/  
  28.         @Override  
  29.         public void onServiceDisconnected(ComponentName arg0) {  
  30.         }  
  31.           
  32.         /*当启动源跟Service成功连接之后将会自动调用这个方法*/  
  33.         @Override  
  34.         public void onServiceConnected(ComponentName name, IBinder binder) {  
  35.             service=((MyBinder)binder).getService();//得到服务对象  
  36.         }  
  37.     };  
  38.   
  39.     @Override  
  40.     protected void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.activity_main);  
  43.   
  44.         getId();  
  45.         bindClick();  
  46.     }  
  47.   
  48.     private void getId() {  
  49.         btStart = (Button) findViewById(R.id.btStart);  
  50.         btStop = (Button) findViewById(R.id.btStop);  
  51.         btBind = (Button) findViewById(R.id.btBind);  
  52.         btPrevious = (Button) findViewById(R.id.btPrevious);  
  53.         btNext = (Button) findViewById(R.id.btNext);  
  54.         btPlay = (Button) findViewById(R.id.btPlay);  
  55.         btPause = (Button) findViewById(R.id.btPause);  
  56.         btUnBind = (Button) findViewById(R.id.btUnBind);  
  57.     }  
  58.   
  59.     private void bindClick() {  
  60.         btStart.setOnClickListener(this);  
  61.         btStop.setOnClickListener(this);  
  62.         btBind.setOnClickListener(this);  
  63.         btPrevious.setOnClickListener(this);  
  64.         btNext.setOnClickListener(this);  
  65.         btPlay.setOnClickListener(this);  
  66.         btPause.setOnClickListener(this);  
  67.         btUnBind.setOnClickListener(this);  
  68.     }  
  69.   
  70.     @Override  
  71.     public void onClick(View view) {  
  72.         switch (view.getId()) {  
  73.         case R.id.btStart:  
  74.             intentStart=new Intent(MainActivity.this,myStartService.class);  
  75.             startService(intentStart);  
  76.             break;  
  77.         case R.id.btStop:  
  78.             stopService(intentStart);  
  79.             break;  
  80.               
  81.         case R.id.btPlay:  
  82.             service.play();  
  83.             break;  
  84.         case R.id.btPause:  
  85.             service.pause();  
  86.             break;  
  87.         case R.id.btPrevious:  
  88.             service.previous();  
  89.             break;  
  90.         case R.id.btNext:  
  91.             service.next();  
  92.             break;  
  93.               
  94.         case R.id.btBind://绑定  
  95.             intentBind=new Intent(MainActivity.this,myBindService.class);  
  96.             //第一个参数为实例的intent对象,第二个参数为得到的服务对象  
  97.             bindService(intentBind, conn,Service.BIND_AUTO_CREATE);  
  98.             break;  
  99.         case R.id.btUnBind://解绑,只可点击一次,并且如果想要结束启动源,必须解绑  
  100.             unbindService(conn);  
  101.             break;  
  102.         }  
  103.     }  
  104. }  


6.运行就显示目标效果了。
0 0
原创粉丝点击