Android——组件之Service

一、Service

    -后台运行,不可见,没有界面

    -优先级高于Activity,Android系统在内存不足时会杀一些等级比较低的Activity

 1. 用途:播放音乐、记录地理信息位置的改变、监听某种动作...

 2. 注意:-Service运行在主线程,不能用它来做耗时的请求或者动作。

            -可以在服务中开一个线程,在线程中做耗时动作

 3. 类型

     (1)本地服务(Local Service) 应用程序内部

           -start方式启动:startService ;stopService   stopSelf   stopSelResult

                服务跟启动源没有任何联系、无法得到服务对象

           -Bind方式启动 bindService ; unbindService  停止前先解绑定

                通过Ibinder接口实例,返回一个ServiceConnection对象给启动源、通过ServiceConnection对象的相关方法可以得到Service对象。

     (2)远程服务(Remote Service)Android系统内部的应用程序之间     

           -定义IBinder接口

二、实例

1.start方式启动Service

   新建Service的派生类,命名为MyStartService ,重写onBind()

public class MyStartService extends Service{

  public IBinder onBind(Intent intent){
     return ;
  }

//在权限xml文件中的application标签内添加


public void onCreate(){
}

public int onStartCommand(Intent intent,int flags,int startId){
}

public void onDestroy(){
}
}

//在Acitvity中启动Service
Intent intent1 = new Intent(xxxActivity.this,MyStartService.class);
startService(intent1);


//停止Service
stopService(intent1);

2.bind方式启动Service

//新建BindService派生类,MyBindService
public class MyBindService extends Service{
  public IBinder onBind(Intent intent){
    
  }
  public void onCreate(){
  }
  public void onDestroy(){
  }
  public void unbindService(ServiceConnection conn){

  }
}
//同样需要注册在application标签内
 
  
intent2 = new Intent(xxxActivity.this,MyBindService.class);
bindService(intent2,null,Service.BIND_AUTO_CREATE);

//解绑定服务,不能连续多次unbind
unbindService(null);
完整的代码

package com.example.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyBindService extends Service{
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.i("info", "BindService--onCreate()");
		super.onCreate();
	}
	public class MyBinder extends Binder{
		public MyBindService getService(){
			return MyBindService.this;
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i("info", "BindService--onBind()");
		return new MyBinder();
	}
	@Override
	public void unbindService(ServiceConnection conn) {
		// TODO Auto-generated method stub
		Log.i("info", "BindService--unbindService()");
		super.unbindService(conn);
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.i("info", "BindService--onDestroy()");
		super.onDestroy();
	}
	public void Play(){
		Log.i("info", "播放");
	}
	public void Pause(){
		Log.i("info", "暂停");
	}
	public void Pervious(){
		Log.i("info", "上一首");
	}
	public void next(){
		Log.i("info", "下一首");
	}
}

package com.example.servicedemo;

import com.example.servicedemo.MyBindService.MyBinder;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	Intent intent1;
	Intent intent2;
	MyBindService service;
	ServiceConnection conn = new ServiceConnection() {
		
		@Override//当服务跟启动源断开的时候 会自动回调
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
		@Override//当服务跟启动源连接的时候 会自动回调
		public void onServiceConnected(ComponentName name, IBinder binder) {
			// TODO Auto-generated method stub
			service = ((MyBinder)binder).getService();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	public void doClick(View v){
		switch (v.getId()) {
		case R.id.start:
			 intent1 = 	new Intent(MainActivity.this, MyStartService.class);
			startService(intent1);
			break;

		case R.id.stop:
			stopService(intent1);
			break;
		case R.id.play:
			service.Play();
			break;
		case R.id.pause:
			service.Pause();
			break;
		case R.id.pervious:
			service.Pervious();
			break;
		case R.id.next:
			service.next();
			break;
		case R.id.bind:
			intent2 = new Intent(MainActivity.this, MyBindService.class);
			startService(intent2);
			bindService(intent2, conn, Service.BIND_AUTO_CREATE);
			break;
		case R.id.unbind:
			unbindService(conn);
			break;
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		stopService(intent2);
		unbindService(conn);
		super.onDestroy();
	}
	@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;
	}

}




    -

你可能感兴趣的:(Android,Android,Service)