service使用以及startService,stopService,bindService,unbindService方法比较
2014-07-14 17:14
543 查看
Service概述
由于手机屏幕的限制,通常情况下在同一时刻仅有一个应用程序处于激活状态,并能够显示在手机屏幕上,因此,应用程序需要一种机制,在没有用户界面的情况下,能够长时间在后台运行,实现应用程序的特定功能,并能够处理事件或更新数据.Android系统提供了(Service)服务组件,它不直接与用户进行交互,却能够长期在后台运行。有很多情况需要使用Service,典型的例子就是:MP3播放器。
Service非常适用于无需用户干预,且需要长期运行的后台功能。Service没有用户界面,有利于降低系统资源。而且Service比Activity具有更高的优先级,只有在系统资源极度匮乏的情况下,android系统才可能清理掉一部分service来保证系统的运行,而这种情况却又轻易不会出现。即使Service被系统终止了,在系统资源恢复后Service也将自动恢复运行状态,因此可以认为Service是在系统中永久运行的组件。Service除了实现后台服务功能,还可以用于进程间通信,解决两个不同Activity应用程序进程之间的调用和通信问题。
Service的生命周期如下图所示:
下面我们用简单地例子来讲解一下如何启动Service
界面如下图所示
实现该界面的xml文件的代码如下:
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopService" />
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unBindService" />
</LinearLayout>
Activity的代码如下:
[java]
view plaincopyprint?
package com.yongchun.intent.ui; /* * bindService和startService都是启动Service,有什么地方不一样呢: * * 1. startService Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系, * 即使调用者退出了,服务依然在进行 * 【onCreate()->onStartCommand()->startService()->onDestroy()】, * 注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。 * ###################################################################### * 2. bindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起, 调用者一旦退出服务也就终止了 * 【onCreate()->onBind()->onUnbind()->onDestroy()】。 * * 鱼说:简而言之,我们做个最简单的测试 第一步:使用startService()启动服务播放音乐,退出应用程序后,音乐还在播放. * 第二步:使用bindService()启动服务播放音乐,退出应用程序后,音乐的播放也随之停止了. */ 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.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载布局文件 setContentView(R.layout.main); // 获取对控件的引用,并设置监听 findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.unbind).setOnClickListener(this); } // 创建一个 ServiceConnection 对象 final ServiceConnection connection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub } }; // 监听事件 public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, PlayRadioService.class); switch (v.getId()) { // startService()方法开启服务 case R.id.start: startService(intent); break; // stopService()方法停止服务 case R.id.stop: stopService(intent); break; // bindService()方法开启服务 case R.id.bind: bindService(intent, connection, BIND_AUTO_CREATE); break; // unbindService()方法停止服务 case R.id.unbind: unbindService(connection); break; default: break; } } }
Activity的开头部分详细介绍了startService以及bindService用法的区别,这里不再累述。
接下来是Service的代码:
[java]
view plaincopyprint?
package com.yongchun.intent.ui;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
/**
* 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com
* 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养.
*/
public class PlayRadioService extends Service {
private MediaPlayer player;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.v("PlayRadioSevice", "Service onCreat()执行成功");
// 创建一个MediaPlayer的对象,并将多媒体文件加载.
player = MediaPlayer.create(this, R.raw.test);
// setLooping(boolean);方法,接受的是boolean值
// true 表示循环播放
// false 表示不循环播放
// 默认值为false,即不循环播放
player.setLooping(true);
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
// MediaPlayer对象的start()方法
player.start();
Log.v("音乐播放", "开始");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
// MediaPlayer对象的stop()方法
player.stop();
Log.v("音乐播放", "停止");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// MediaPlayer对象的stop()方法
Log.v("音乐播放", "停止#####onUnbind");
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
// MediaPlayer对象的start()方法
player.start();
Log.v("音乐播放", "开始####onBind");
return null;
}
}
对应的清单文件如下所示,需要将定义的Service在清单文件中进行注册。
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yongchun.intent.ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
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=".PlayRadioService" >
</service>
</application>
</manifest>
由于手机屏幕的限制,通常情况下在同一时刻仅有一个应用程序处于激活状态,并能够显示在手机屏幕上,因此,应用程序需要一种机制,在没有用户界面的情况下,能够长时间在后台运行,实现应用程序的特定功能,并能够处理事件或更新数据.Android系统提供了(Service)服务组件,它不直接与用户进行交互,却能够长期在后台运行。有很多情况需要使用Service,典型的例子就是:MP3播放器。
Service非常适用于无需用户干预,且需要长期运行的后台功能。Service没有用户界面,有利于降低系统资源。而且Service比Activity具有更高的优先级,只有在系统资源极度匮乏的情况下,android系统才可能清理掉一部分service来保证系统的运行,而这种情况却又轻易不会出现。即使Service被系统终止了,在系统资源恢复后Service也将自动恢复运行状态,因此可以认为Service是在系统中永久运行的组件。Service除了实现后台服务功能,还可以用于进程间通信,解决两个不同Activity应用程序进程之间的调用和通信问题。
Service的生命周期如下图所示:
下面我们用简单地例子来讲解一下如何启动Service
界面如下图所示
实现该界面的xml文件的代码如下:
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopService" />
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unBindService" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startService" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stopService" /> <Button android:id="@+id/bind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bindService" /> <Button android:id="@+id/unbind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="unBindService" /> </LinearLayout>
Activity的代码如下:
[java]
view plaincopyprint?
package com.yongchun.intent.ui; /* * bindService和startService都是启动Service,有什么地方不一样呢: * * 1. startService Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系, * 即使调用者退出了,服务依然在进行 * 【onCreate()->onStartCommand()->startService()->onDestroy()】, * 注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。 * ###################################################################### * 2. bindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起, 调用者一旦退出服务也就终止了 * 【onCreate()->onBind()->onUnbind()->onDestroy()】。 * * 鱼说:简而言之,我们做个最简单的测试 第一步:使用startService()启动服务播放音乐,退出应用程序后,音乐还在播放. * 第二步:使用bindService()启动服务播放音乐,退出应用程序后,音乐的播放也随之停止了. */ 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.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载布局文件 setContentView(R.layout.main); // 获取对控件的引用,并设置监听 findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.unbind).setOnClickListener(this); } // 创建一个 ServiceConnection 对象 final ServiceConnection connection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub } }; // 监听事件 public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, PlayRadioService.class); switch (v.getId()) { // startService()方法开启服务 case R.id.start: startService(intent); break; // stopService()方法停止服务 case R.id.stop: stopService(intent); break; // bindService()方法开启服务 case R.id.bind: bindService(intent, connection, BIND_AUTO_CREATE); break; // unbindService()方法停止服务 case R.id.unbind: unbindService(connection); break; default: break; } } }
package com.yongchun.intent.ui; /* * bindService和startService都是启动Service,有什么地方不一样呢: * * 1. startService Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系, * 即使调用者退出了,服务依然在进行 * 【onCreate()->onStartCommand()->startService()->onDestroy()】, * 注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。 * ###################################################################### * 2. bindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起, 调用者一旦退出服务也就终止了 * 【onCreate()->onBind()->onUnbind()->onDestroy()】。 * * 鱼说:简而言之,我们做个最简单的测试 第一步:使用startService()启动服务播放音乐,退出应用程序后,音乐还在播放. * 第二步:使用bindService()启动服务播放音乐,退出应用程序后,音乐的播放也随之停止了. */ 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.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载布局文件 setContentView(R.layout.main); // 获取对控件的引用,并设置监听 findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.unbind).setOnClickListener(this); } // 创建一个 ServiceConnection 对象 final ServiceConnection connection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub } }; // 监听事件 public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, PlayRadioService.class); switch (v.getId()) { // startService()方法开启服务 case R.id.start: startService(intent); break; // stopService()方法停止服务 case R.id.stop: stopService(intent); break; // bindService()方法开启服务 case R.id.bind: bindService(intent, connection, BIND_AUTO_CREATE); break; // unbindService()方法停止服务 case R.id.unbind: unbindService(connection); break; default: break; } } }
Activity的开头部分详细介绍了startService以及bindService用法的区别,这里不再累述。
接下来是Service的代码:
[java]
view plaincopyprint?
package com.yongchun.intent.ui;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
/**
* 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com
* 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养.
*/
public class PlayRadioService extends Service {
private MediaPlayer player;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.v("PlayRadioSevice", "Service onCreat()执行成功");
// 创建一个MediaPlayer的对象,并将多媒体文件加载.
player = MediaPlayer.create(this, R.raw.test);
// setLooping(boolean);方法,接受的是boolean值
// true 表示循环播放
// false 表示不循环播放
// 默认值为false,即不循环播放
player.setLooping(true);
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
// MediaPlayer对象的start()方法
player.start();
Log.v("音乐播放", "开始");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
// MediaPlayer对象的stop()方法
player.stop();
Log.v("音乐播放", "停止");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// MediaPlayer对象的stop()方法
Log.v("音乐播放", "停止#####onUnbind");
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
// MediaPlayer对象的start()方法
player.start();
Log.v("音乐播放", "开始####onBind");
return null;
}
}
package com.yongchun.intent.ui; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; /** * 作者:肥鱼 QQ群:104780991 Email:zhaoyongchun2011@gmail.com * 关于:一条致力于Android开源事业的鱼,还是肥的.吃得多赚的少还不会暖床,求包养. */ public class PlayRadioService extends Service { private MediaPlayer player; @Override public void onCreate() { // TODO Auto-generated method stub Log.v("PlayRadioSevice", "Service onCreat()执行成功"); // 创建一个MediaPlayer的对象,并将多媒体文件加载. player = MediaPlayer.create(this, R.raw.test); // setLooping(boolean);方法,接受的是boolean值 // true 表示循环播放 // false 表示不循环播放 // 默认值为false,即不循环播放 player.setLooping(true); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub // MediaPlayer对象的start()方法 player.start(); Log.v("音乐播放", "开始"); super.onStart(intent, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub // MediaPlayer对象的stop()方法 player.stop(); Log.v("音乐播放", "停止"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub // MediaPlayer对象的stop()方法 Log.v("音乐播放", "停止#####onUnbind"); return super.onUnbind(intent); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub // MediaPlayer对象的start()方法 player.start(); Log.v("音乐播放", "开始####onBind"); return null; } }
对应的清单文件如下所示,需要将定义的Service在清单文件中进行注册。
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yongchun.intent.ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
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=".PlayRadioService" >
</service>
</application>
</manifest>
相关文章推荐
- service使用以及startService,stopService,bindService,unbindService方法比较
- service使用以及startService,stopService,bindService,unbindService方法比较
- service使用以及startService,stopService,bindService,unbindService方法比较
- Service组件(startService,stopService,bindService,unbindService方法比较)-Android
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- 调用android非unbind的服务中的方法(不使用bindService启动的服务)
- BindService的生命周期以及使用方法。
- 使用service的startservice 和bindservice方法实现音乐的播放、暂停、继续
- startService、stopService、bindService和unbindService的关系
- Android里Service的bindService()和startService()混合使用深入分析
- 在TabActivity中无法使用bindService的解决方法
- android之startService和bindService方法的区别
- Android里Service的bindService()和startService()混合使用深入分析
- 使用bindService和服务的通信(bindService和startService的区别)
- 通过startService()方法使用Service
- Android中bindService的使用方法
- Android里Service的bindService()和startService()混合使用深入分析
- mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法