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

Android四大天王之Service篇

2016-07-12 11:38 337 查看
android 四大天王分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器。本篇博客是关于service的介绍。service是在一段不定的时间运行在后台,不和用户交互应用组件。每个service必须在Androidmainfest.xml中用<service>声明。
service 有两种启动模式startService()和bindService(); startService()不和activity交互,另一种模式和activity进行交互。

生命周期:使用startService()启动service会经历:startService() ,onCreat(),onStart(),service running,停止会经历stopService() ,onDestroy(),service stop使用bindService(),onCreat(),onBind(),service running ,停止会经历onunbind() ,onDestroy(),service stop。onBind会返回客户端一个IBind接口实例,通过这个接口实例就可以获取服务,进而和activity进行交互。示例代码如下:

package com.example.service;

import com.example.service.TestService.MyBinder;

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.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {
Intent intent=null;
TestService test_service=null;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent(this,TestService.class);
}

ServiceConnection sc=new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder binder=(MyBinder)service;
test_service=binder.getService();
System.out.println("ServiceConnection");
}

public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected");
}
};

public void onClick(View v){
int id=v.getId();
switch (id) {
case R.id.start:
this.startService(intent);
break;
case R.id.stop:
this.stopService(intent);
break;
case R.id.binderService:
bindService(intent, sc, Service.BIND_AUTO_CREATE);
break;
case R.id.unBinderService:
unbindService(sc);
break;

default:
break;
}

}
}

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

public class TestService extends Service {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate();

}

public IBinder onBind(Intent arg0) {

return new MyBinder();
}

class MyBinder extends Binder{
public TestService getService(){
return TestService.this;
}
}

public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onstart");
return super.onStartCommand(intent, flags, startId);
}

public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.service.MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="服务测试" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动服务"
android:id="@+id/start"
android:onClick="onClick"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务"
android:id="@+id/stop"
android:onClick="onClick"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
android:id="@+id/binderService"
android:onClick="onClick"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解绑服务"
android:id="@+id/unBinderService"
android:onClick="onClick"
/>
</LinearLayout>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息