Android Service基本知识

概念:

Service是应用组件(运行在宿主进程的主线程中,或重新开启一个子线程),执行长时间运行操作,或为其它应用提供功能函数,每个Service必须在工程的AndroidManifest.xml中进行声明,开启Service以Context.startService()或Context.bindService()方式。

生命周期:



 

 

=========MainActivity.java==============

package com.xuwan.service;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

扫描二维码关注公众号,回复: 602746 查看本文章

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;

/**

 * 本地Service

 * 

 * @author SJC

 * 

 */

public class MainActivity extends Activity implements OnClickListener {

private static String tag = "MainActivity";

private LocalService localService = null;

private Boolean isBind = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button startServiceButton = (Button) findViewById(R.id.startService);

Button stopServiceButton = (Button) findViewById(R.id.stopService);

Button bindServiceButton = (Button) findViewById(R.id.bindService);

Button unbindServiceButton = (Button) findViewById(R.id.unbindService);

startServiceButton.setOnClickListener(this);

stopServiceButton.setOnClickListener(this);

bindServiceButton.setOnClickListener(this);

unbindServiceButton.setOnClickListener(this);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public void onClick(View v) {

if (v.getId() == R.id.startService) {

Log.i(tag, "==========启动服务==========");

Intent service = new Intent(this, LocalService.class);

startService(service);

} else if (v.getId() == R.id.stopService) {

Log.i(tag, "==========ֹͣ停止服务==========");

Intent service = new Intent(this, LocalService.class);

stopService(service);

} else if (v.getId() == R.id.bindService) {

Log.i(tag, "==========ֹͣ绑定服务==========");

Intent service = new Intent(this, LocalService.class);

bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);

isBind = true;

} else if (v.getId() == R.id.unbindService) {

Log.i(tag, "==========ֹͣ卸载服务==========");

if (isBind) {

unbindService(serviceConnection);

isBind = false;

}

}

}

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

Log.i(tag, "onServiceDisconnected===" + "name:" + name);

localService = null;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

Log.i(tag, "onServiceConnected===" + "name:" + name);

localService = ((LocalService.LocalBinder) service)

.getLocalService();

}

};

}

=========LocalService.java==============

/**

 * 

 */

package com.xuwan.service;

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

/**

 * @author SJC

 * 

 */

public class LocalService extends Service {

private static String tag = "LocalService";

private Binder binder = new LocalService.LocalBinder();

private MediaPlayer mp = null;

@Override

public IBinder onBind(Intent intent) {

Log.i(tag, "onBind");

return binder;

}

@Override

public void onCreate() {

Log.i(tag, "onCreate");

mp = MediaPlayer.create(this, R.raw.dongshi_sunyanzi);

mp.start();

super.onCreate();

}

@Override

public void onStart(Intent intent, int startId) {

Log.i(tag, "onStart");

super.onStart(intent, startId);

}

/**

* <1>START_CONTINUATION_MASK<br/>

* <2>START_FLAG_REDELIVERY<br/>

* <3>START_FLAG_RETRY<br/>

* <4>START_NOT_STICKY 如果进程被kill掉,系统将不会自动重新该Service<br/>

* <5>START_REDELIVER_INTENT

* 如果进程被kill掉,保留Service的状态和intent对象,随后系统会自动重启Service<br/>

* <6>START_STICKY 如果进程被kill掉,保留Service的状态为开始状态,但不会保留传递的intent对象;

* 随后系统会自动重启Service<br/>

* <7>START_STICKY_COMPATIBILITY START_STICKY的兼容版本,

* 并不能保证进程被kill掉后会调用onStartCommand()方法<br/>

*/

@Override

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

Log.i(tag, "onStartCommand" + "|" + "flags:" + flags + "|" + "startId:"

+ startId);

return START_STICKY;

}

@Override

public boolean onUnbind(Intent intent) {

Log.i(tag, "onUnbind");

return super.onUnbind(intent);

}

@Override

public void onDestroy() {

Log.i(tag, "onDestroy");

mp.stop();

super.onDestroy();

}

public class LocalBinder extends Binder {

LocalService getLocalService() {

return LocalService.this;

}

}

}

<1>当执行“启动服务”时,打印信息如下:



 

<2>当执行“停止服务”时,打印信息如下:



 

<3>当执行“绑定服务”时,打印信息如下:



 

<4>当执行“卸载服务”时,打印信息如下:



 

猜你喜欢

转载自shenjichao2009.iteye.com/blog/2019706
今日推荐