1.前言:前面学习了Android四大组件之一的Activity接下来是Service。
2.目录:
1)Service概述(它是啥?)
2)Service的用途(它能干啥?)
3)Service类型
4)Service的生命周期(附logcat打印)
5)Service的使用(附demo)
2.Service概述:Android四大组件之一,用于执行后台的耗时操作,不提供用户界面,它能被其他应用程序启动。
3.Service的用途
3.1)绑定其他组件,进行交互
3.2)IPC(Inter-Process Communication 进程间通信)
3.3)执行耗时操作
3.4)应用:处理网络传输、音乐播放、文件I/O、与content provider交互
4. Service类型
4.1 Started类型:由startService()启动,不随启动他的组件销毁而销毁,一般也不会向调用者返回结果,服务完成后自动终止。
4.2 Bound类型:由bindService()绑定,提供client/serve接口进行组件与服务的交互,可进行IPC跨进程操作,随绑定组件销毁而销毁
PS:两种service可以同时使用,同时一个应用程序的service也可以被另一个应用程序调用,声明为私有可阻止其他应用调用
5. Service的生命周期(代码见6 Service的使用)
5.1 startService()启动Service生命周期
5.2 bindService()启动Service生命周期
6. Service的使用(附demo)
ps:demo可参考:http://blog.csdn.net/conowen/article/details/7272018 bug:越过bindService()执行unbindService()会出现Service为注册的错误,解决参考:http://blog.csdn.net/loongggdroid/article/details/18041147
2.目录:
1)Service概述(它是啥?)
2)Service的用途(它能干啥?)
3)Service类型
4)Service的生命周期(附logcat打印)
5)Service的使用(附demo)
2.Service概述:Android四大组件之一,用于执行后台的耗时操作,不提供用户界面,它能被其他应用程序启动。
3.Service的用途
3.1)绑定其他组件,进行交互
3.2)IPC(Inter-Process Communication 进程间通信)
3.3)执行耗时操作
3.4)应用:处理网络传输、音乐播放、文件I/O、与content provider交互
4. Service类型
4.1 Started类型:由startService()启动,不随启动他的组件销毁而销毁,一般也不会向调用者返回结果,服务完成后自动终止。
4.2 Bound类型:由bindService()绑定,提供client/serve接口进行组件与服务的交互,可进行IPC跨进程操作,随绑定组件销毁而销毁
PS:两种service可以同时使用,同时一个应用程序的service也可以被另一个应用程序调用,声明为私有可阻止其他应用调用
5. Service的生命周期(代码见6 Service的使用)
5.1 startService()启动Service生命周期
startService()->onCreate()->onStartCommand()->onStart->stopService()->onDestroy()
5.2 bindService()启动Service生命周期
bindService->onCreate()->unbindService()->onUnbind()->onDestroy()
6. Service的使用(附demo)
6.1)建立组件ServiceActivity.java继承Activity
package com.test.servicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ServiceActivity extends Activity {
Button startService = null;
Button stopService = null;
Button bindService = null;
Button unbindService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
startService = (Button) findViewById(R.id.start);
stopService = (Button) findViewById(R.id.stop);
bindService = (Button) findViewById(R.id.bind);
unbindService = (Button) findViewById(R.id.unbind);
final Intent intent = new Intent(ServiceActivity.this, MyService.class);
/*
* startService()启动服务
*/
// 设置startService button监听
startService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(intent);
System.out.println("Started->startService");
}
});
// 设置stopService button监听
stopService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
System.out.println("Started->stopService");
}
});
/*
* bindService()绑定服务
*/
// bindService的时候会调用onServiceConnected
// unbindService的时候回调用onServiceDisconnected
final ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(ServiceActivity.this, "onServiceConnected",
Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(ServiceActivity.this, "onServiceDisconnected",
Toast.LENGTH_LONG).show();
}
};
// 设置bindService button监听
bindService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent, conn, BIND_AUTO_CREATE);
System.out.println("Bound->bindService");
}
});
// 设置unbindService button监听
unbindService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);
System.out.println("Bound->unbindService");
}
});
}
}
6.2)建服务类MyService继承Service
package com.test.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("MyService->onCreate");
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
System.out.println("MyService->onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("MyService->onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
System.out.println("MyService->onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("MyService->onUnbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
System.out.println("MyService->onRebind");
super.onRebind(intent);
}
}
6.3)建布局xml activity_service.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.servicedemo.ServiceActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/startService" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stopService" />
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bindService" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unbindService" />
</LinearLayout>
6.3)Manifest.xml注册服务
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.servicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ServiceActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" >
</service>
</application>
</manifest>
ps:demo可参考:http://blog.csdn.net/conowen/article/details/7272018 bug:越过bindService()执行unbindService()会出现Service为注册的错误,解决参考:http://blog.csdn.net/loongggdroid/article/details/18041147