Android的Service学习之一(简单的Service的框架)
2013-02-26 16:00
483 查看
Service是android 系统中的一种组件,它跟Activity的级别差不多,但是他不能自己运行,只能后台运行,并且可以和其他组件进行交互。Service的启动有两种方式:context.startService() 和 context.bindService()。
使用context.startService() 启动Service是会会经历:
context.startService() ->onCreate()- >onStart()->Service running
context.stopService() | ->onDestroy() ->Service stop
如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。
stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
使用使用context.bindService()启动Service会经历:
context.bindService()->onCreate()->onBind()->Service running
onUnbind() -> onDestroy() ->Service stop
onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。
在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
1. 文件 MyService.java
public class MyService extends Service{
private static String TAG="MyService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG, "onCreate()...");
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i(TAG, "onStart()...");
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
Log.i(TAG, "finalize()...");
}
}
2. 文件 Androidnew03Activity.java
public class Androidnew03Activity extends Activity {
private Button btn1 = null, btn2=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)this.findViewById(R.id.btnStartService);
btn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(new Intent(Androidnew03Activity.this, MyService.class));
}
});
btn2 = (Button)this.findViewById(R.id.btnStopService);
btn2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopService(new Intent(Androidnew03Activity.this, MyService.class));
}
});
}
}
3. 文件 main.xml
<?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/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startservice"
/>
<Button
android:id="@+id/btnStopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stopservice"
/>
</LinearLayout>
使用context.startService() 启动Service是会会经历:
context.startService() ->onCreate()- >onStart()->Service running
context.stopService() | ->onDestroy() ->Service stop
如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。
stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
使用使用context.bindService()启动Service会经历:
context.bindService()->onCreate()->onBind()->Service running
onUnbind() -> onDestroy() ->Service stop
onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。
在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
1. 文件 MyService.java
public class MyService extends Service{
private static String TAG="MyService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG, "onCreate()...");
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i(TAG, "onStart()...");
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
Log.i(TAG, "finalize()...");
}
}
2. 文件 Androidnew03Activity.java
public class Androidnew03Activity extends Activity {
private Button btn1 = null, btn2=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)this.findViewById(R.id.btnStartService);
btn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(new Intent(Androidnew03Activity.this, MyService.class));
}
});
btn2 = (Button)this.findViewById(R.id.btnStopService);
btn2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopService(new Intent(Androidnew03Activity.this, MyService.class));
}
});
}
}
3. 文件 main.xml
<?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/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startservice"
/>
<Button
android:id="@+id/btnStopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stopservice"
/>
</LinearLayout>
相关文章推荐
- android Service 的简单使用(学习别人)
- android 媒体框架学习--mediaplayerservice 关系类图
- 【Android学习日记】(八) Service的简单使用例子
- 从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-2.service)
- 一个简单的demo学习Android远程Service(AIDL的使用
- 一个简单的demo学习Android远程Service(AIDL的使用)
- 一个简单的demo学习Android远程Service(AIDL的使用)
- 一个简单的demo学习Android远程Service(AIDL的使用)
- android Service 的简单使用(学习别人)
- 一个简单的demo学习Android远程Service(AIDL的使用)
- Android 初步学习BroadCast与Service实现简单的音乐播放器
- Android学习路线(四)构建一个简单的UI
- React-Native学习笔记之:弹框框架Popover简单使用
- Android 开源框架Universal-Image-Loader学习
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- android学习ViewPager的简单使用
- android框架简单剖析~
- android 学习随笔九(网络:简单新闻客户端实现)
- android 25 底部导航框架学习 BottomNavigationView
- Android学习之远程绑定调用service