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>
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>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析