Android Service组件(1)
android service 和其他服务一样,并没有实际运行的界面,它运行在android 后台。一般通过service为应用程序提供服务(比如,从Internet下载文件,控制音乐播放器等)。Service的生命周期要比activity简单的多,它只有三个阶段(创建服务、开始服务、销毁服务)。下面通过具体事例讲解android的service组件。
1.创建android工程
在Eclipse中创建android工程 android_service_one
2.创建Service
在android工程中,创建包com.example.service,并添加方法MyService。该方法继承与Service。该类用来展示服务的三个生命周期。
package com.example.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;/* * */public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } //第一次打开时调用 public void onCreate() { Log.d("MyService", "onCreate"); super.onCreate(); } //停止时调用 public void onDestory() { Log.d("MySerVice", "onDestory"); super.onDestroy(); } //开始时调用 public void onStart(Intent intent,int startId) { Log.d("MyService", "onStart"); super.onStart(intent, startId); }}MyService
3.调用服务
在android 活动组件中调用刚刚建立的服务,调用之前需要在AndroidManifest.xml中对该服务进行配置。通过<service></service>来配置服务组件。
1 <service android:enabled="true" android:name="com.example.service.MyService"></service>
服务配置完成以后,可以调用该服务。服务与activity之间的交互依然通过Intent来进行通信。启动服务通过startservice(intent),停止服务通过stopservice(Intent intent)来完成。
Intent的声明:Intent Intent=new Intent(this,MyService.class);
1 public void onClick(View v) { 2 // TODO Auto-generated method stub 3 switch(v.getId()) 4 { 5 case R.id.button1: 6 startService(serviceintent); 7 break; 8 case R.id.button2: 9 stopService(serviceintent);10 break;11 }12 }服务的停止与启动
4.Service与Activity绑定
Service与Activity绑定,当Activity启动时,服务自动启动。当Activity被销毁时,服务也被销毁。这里需要在MyService中重写 onBind、onRebind和onUnbind等几个方法。
1 package com.example.service; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.util.Log; 8 9 /*10 * 11 */12 public class MyService extends Service {13 14 private MyBinder myBinder= new MyBinder();15 16 @Override17 //成功绑定时调用该方法18 public IBinder onBind(Intent intent) {19 // TODO Auto-generated method stub20 Log.d("MyService", "onBind");21 return myBinder;22 }23 24 //重新绑定时调用该方法25 public void onRebind(Intent intent)26 {27 Log.d("MyService", "onRebind");28 super.onRebind(intent);29 }30 31 //解除绑定时调用该方法32 public void onUnBind(Intent intent)33 {34 Log.d("MyService", "onUnbind");35 super.onUnbind(intent);36 }37 //第一次打开时调用38 public void onCreate()39 {40 Log.d("MyService", "onCreate");41 super.onCreate();42 }43 //停止时调用44 public void onDestory()45 {46 Log.d("MySerVice", "onDestory");47 super.onDestroy();48 }49 //开始时调用50 public void onStart(Intent intent,int startId)51 {52 Log.d("MyService", "onStart");53 super.onStart(intent, startId);54 }55 56 //绑定类57 public class MyBinder extends Binder58 {59 //返回服务实例60 public MyService getService()61 {62 return MyService.this;63 }64 }65 }MyService
5.调用服务
在Activity中声明服务变量 Myservice myService,和声明ServiceConnection连接变量。绑定变量调用bindService(serviceintent, conn, Context.BIND_AUTO_CREATE);
解除绑定调用,unbindService(ServiceConnection).
1 Intent serviceintent; 2 private MyService myService; 3 private ServiceConnection conn=new ServiceConnection() { 4 5 @Override 6 public void onServiceDisconnected(ComponentName name) { 7 // TODO Auto-generated method stub 8 myService=null; 9 Toast.makeText(MainActivity.this, "服务连接失败", Toast.LENGTH_LONG).show();10 }11 12 @Override13 public void onServiceConnected(ComponentName name, IBinder service) {14 // TODO Auto-generated method stub15 myService=((MyService.MyBinder)service).getService();16 Toast.makeText(MainActivity.this, "服务连接成功", Toast.LENGTH_LONG).show();17 }18 };ServiceConnection
更多相关文章
- Android实现全屏显示的方法 固定横屏或者竖屏的方法
- 关于安装Android Studio的一些问题的解决方法
- android 按钮点击的两种方法以及长按事件
- Android 使用Handler的PostDelayed方法实现图片的轮播
- Android 应用启动闪白一下处理方法