1 生命周期图
2 第一种 非绑定方式 startService
2.1 自定义 Service
- package com.demoservice;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
- public class FirstService extends Service {
- MediaPlayer mplayer;
- Binder binder=new Binder();
- @Override
- public IBinder onBind(Intent intent) {
- // TODO 自动生成的方法存根
- return binder;
- }
- @Override
- public void onCreate() {
- // TODO 自动生成的方法存根
- super.onCreate();
- mplayer=MediaPlayer.create(this, R.raw.music12);
- Log.i("demo", "onCreate");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO 自动生成的方法存根
- Log.i("demo", "onStartCommand");
- mplayer.start();
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- // TODO 自动生成的方法存根
- Log.i("demo", "onDestroy");
- mplayer.stop();
- super.onDestroy();
- }
- }
2.2 注册 Service
- <service
- android:name="com.demoservice.FirstService">
- <intent-filter >
- <action android:name="michael.FIRSTSERVICE"/>
- </intent-filter>
- </service>
2.3 开启/关闭 Service
- package com.demoservice;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- Button btn_start,btn_stop;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn_start=(Button)findViewById(R.id.btn_start);
- btn_stop=(Button)findViewById(R.id.btn_stop);
- final Intent intent=new Intent();
- intent.setAction("michael.FIRSTSERVICE");
- OnClickListener listener=new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO 自动生成的方法存根
- if(v == btn_start)
- {
- startService(intent);
- }else if(v==btn_stop)
- {
- stopService(intent);
- }
- }
- };
- btn_start.setOnClickListener(listener);
- btn_stop.setOnClickListener(listener);
- }
- }
3 第二种 绑定方式 bindService
3.1 自定义 Service
- package com.SecondService;
- import com.demoservice.R;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
- public class SecondService extends Service {
- int count;
- boolean quit=false;
- MediaPlayer mplayer;
- MyBinder binder=new MyBinder();
- @Override
- public IBinder onBind(Intent intent) {
- // TODO 自动生成的方法存根
- Log.i("demo", "onBind");
- mplayer.start();
- new Thread()
- {
- @Override
- public void run() {
- while(!quit)
- {
- try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
- count++;
- Log.i("demo", String.valueOf(count));
- }
- }
- }.start();
- return binder;
- }
- @Override
- public boolean onUnbind(Intent intent) {
- Log.i("demo", "onUnbind");
- quit=true;
- return super.onUnbind(intent);
- }
- @Override
- public void onRebind(Intent intent) {
- Log.i("demo", "onRebind");
- mplayer.start();
- super.onRebind(intent);
- }
- @Override
- public void onCreate() {
- super.onCreate();
- quit=false;
- mplayer=MediaPlayer.create(this, R.raw.music12);
- Log.i("demo", "onCreate");
- }
- @Override
- public void onDestroy() {
- Log.i("demo", "onDestroy");
- mplayer.stop();
- quit=true;
- super.onDestroy();
- }
- class MyBinder extends Binder
- {
- public SecondService getService()
- {
- return SecondService.this;
- }
- public int getCount()
- {
- return count;
- }
- }
- }
3.2 注册 Service
- <service
- android:name="com.SecondService.SecondService">
- <intent-filter >
- <action android:name="michael.SECONDSERVICE"/>
- </intent-filter>
- </service>
3.3 绑定/解绑Service
- package com.SecondService;
- import com.demoservice.R;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- SecondService mservice;
- ServiceConnection conn=new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO 自动生成的方法存根
- Log.i("demo", "onServiceDisconnected");
- mservice=null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO 自动生成的方法存根
- Log.i("demo", "onServiceConnecteds");
- mservice = ((SecondService.MyBinder)service).getService();
- }
- };
- Button btn_start,btn_stop,btn_get;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn_start=(Button)findViewById(R.id.btn_start);
- btn_get=(Button)findViewById(R.id.btn_get);
- btn_stop=(Button)findViewById(R.id.btn_stop);
- final Intent intent=new Intent();
- intent.setAction("michael.SECONDSERVICE");
- OnClickListener listener=new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO 自动生成的方法存根
- if(v == btn_start)
- {
- bindService(intent, conn, BIND_AUTO_CREATE);
- }else if(v==btn_stop)
- {
- unbindService(conn);
- }else if(v==btn_get)
- {
- btn_get.setText(String.valueOf(mservice.binder.getCount()));
- }
- }
- };
- btn_start.setOnClickListener(listener);
- btn_stop.setOnClickListener(listener);
- btn_get.setOnClickListener(listener);
- }
- }
转载请注明出处!
原:http://blog.csdn.net/mkrcpp/article/details/11993007