(1.)这边是Activity对Service操作的类
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @项目名:ServiceTest_demo1
* @类名称:MainActivity
* @类描述: 服务基本用法
* @创建人:HXF
* @修改人:
* @创建时间:2015-8-13 上午11:22:54
* @version
*
*/
public class MainActivity extends Activity implements OnClickListener {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
/** 拿到服务的对象 */
private MyService.DownLoadBinder downLoadBinder;
/** 匿名类,里面的方法在成功绑定的时候和解除绑定的时候调用 */
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downLoadBinder = (MyService.DownLoadBinder)service;
downLoadBinder.startDownload();
downLoadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/** 初始化控件 */
private void init() {
startService = (Button) findViewById(R.id.start_sercice);
stopService = (Button) findViewById(R.id.stop_sercice);
bindService = (Button) findViewById(R.id.bind_sercice);
unbindService = (Button) findViewById(R.id.UnBind_sercice);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_sercice:
/** 开启服务 */
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
break;
case R.id.stop_sercice:
/** 停止服务 */
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);
break;
case R.id.bind_sercice:
/** 绑定服务 */
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.UnBind_sercice:
/** 解开绑定服务 */
unbindService(connection);
break;
default:
break;
}
}
}
(2.)继承Service解释一些简单的方法的使用
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/** 服务的基本用法和方法的解释 */
public class MyService extends Service{
private DownLoadBinder mBinder = new DownLoadBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** 会在服务创建的时候调用 */
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate executed");
}
/** 会在每次服务启动的时候调用 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}
/** 会在每次服务销毁的时候调用 */
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy executed");
}
/**
*
* @项目名:ServiceTest_demo1
* @类名称:DownLoadBinder
* @类描述: 绑定服务与activity设置
* @创建人:HXF
* @修改人:
* @创建时间:2015-8-13 上午11:52:46
* @version
*
*/
class DownLoadBinder extends Binder{
/** 开始下载 */
public void startDownload(){
System.out.println("开始下载---startDownload");
}
/** 查看下载进度 */
public int getProgress(){
System.out.println("查看下载进度---getProgress");
return 0;
}
}
}