您的位置:首页 > 移动开发 > Android开发

Service 初步 -- MarsChen Android 开发教程学习笔记

2014-09-03 11:19 736 查看
Service 是什么 是Android 非常重要的组件,不可见,通常用来处理耗时比较长的操作。可以使用Service 更新ContentProvider,发送Intent 以及启动系统的通知等等。
Service 不是什么 Service 不是一个单独的进程。 Service 不是一个线程。 一个进程可以包含多个线程,进程占用系统资源,线程占用进程资源。 Service 生命周期 新建一个类,继承自Service ,其中onBind,onCreat,onStartCommand 和onDestroy 方法需要复写。 当启动或创建Service 对象时,就会调用onCreat 方法销毁对象时,用onDestroy 方法,启动Service 的时候调用onStartCommand 方法。第一次启动Service 的时候会调用onCreat 方法,除非停止Service ,第二次以及后续的启动Service 都不会调用onCreat 方法,而是直接调用onStartCommand 方法。 onStartCommmand(Intent intent, int flags, int startId) 方法共有三个参数,第一个是在Activity 中调用的Intent , 具体步骤:onCreat → onStartCommand → ...... → onStop。 需要在AndroidManifest 文件当中添加标签注册Service :
<service android name=".FirstService"></service>

启动和停止Service 的方法 启动和停止,都在Activity 中操作。 启动Service 仍然需要利用Intent 对象。
Intent intent  = new Intent();	intent.setClass(TestActivtiy.this, FirstService.class);	startService(intent);
停止的方法与启动类似。
Intent intent  = new Intent();	intent.setClass(TestActivtiy.this, FirstService.class);	stopService(intent);

什么是bound service bound service 是绑定Service,是有别于启动Service 的另一种方法。
Bound Service 与Start Service 的区别 Activity 得不到由Activity 开启的Service 的反馈。 绑定Service 是Service 形成一个服务器端,允许Activity 或其他客户端进行绑定,接收Service 的信息,与之交互。
如何绑定 创建一个新的类,继承自android.app 的Service 类,复写会返回出IBinder 的onBind 方法。此方法当其它应用程序组件(主要指Activity)绑定至当前的Service 对象时,将会调用该方法。 而在Activity 中实现绑定,需要创建intent 对象,调用setClass 方法,再调用bindService 方法,第一个参数是intent,第二个参数是ServiceConnected 内部类的传入,当Activity 和Service 绑定在一起的时候,就会调用ServiceConected 里面onServiceConnected 方法,反之则用onServiceDisconnected 。
Parcel 对象的基本使用方法 是一个序列化对象,可以轻度序列化,可以理解为一个包裹,什么数据都能放进去。 在Activity 中生成Parcel 对象,调用obtain() 方法。在功能类,比如给按钮用到的内部类中,调用writexxx() 方法,传入数据,xxx 是传入数据的类型,即可存储数据。调用readxxx() 方法,读取数据。 Parcel 中,读取数据和存放数据的指针是相同的,如果存入数据分别为a,b,c,那么指针就指在d,读取时的顺序是c,b,a。通过调用setDataPosition(0) 方法可以将指针放在最前面,这样读取数据不会倒序。
transact 方法与onTransact 方法 transact 方法可以实现Activity 向Service 传送消息,onTransact 是Service 向Activity 传输响应。 新建一个Activity ,创建一个继承自Service 的类,复写onBind 方法,添加一个继承自Binder 的内部类,即可复写onTransact 方法。
public class MainActivity extends ActionBarActivity {

private Button button1;
private Button button2;
private Binder binder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);

ButtonListener listener = new ButtonListener();
button1.setOnClickListener(listener);
SecondButtonListener secondlistener = new SecondButtonListener();
button2.setOnClickListener(secondlistener);
}

class ButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
}

class SecondButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();

data.writeString("(^U^)ノ~YO ");

try {
binder.transact(0, data, reply, 0);
System.out.println("reply-------->" + reply.readString());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

ServiceConnected conn = new ServiceConnected();
class ServiceConnected implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MainActivity.this.binder = (Binder)service;

}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}
@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: