Service的生命周期
来源:互联网 发布:美工设计培训班 编辑:程序博客网 时间:2024/05/18 15:24
Service的生命周期
Service的生命周期如下
第一种:Call to startService()方法->onCreate()->onStartCommand()->service running-
>onDestory->service shut down)
案例:
第一步
第三步配置文件在<application>节点下加入子节点(与activity节点平级):
<service android:name="com.example.simpleservice.SimpleService" >
</service>
Service的生命周期如下
第一种:Call to startService()方法->onCreate()->onStartCommand()->service running-
>onDestory->service shut down)
案例:
第一步
在布局文件添加两个按钮
<Button android:id="@+id/start_service" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:onClick="dostartService" android:text="启动simpleService" /> <Button android:id="@+id/stop_service" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/start_service" android:layout_marginTop="51dp" android:onClick="dostopService" android:text="停止simpleService" />第二步创建SimpleService类继承Service
package com.example.simpleservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class SimpleService extends Service{private String tag ="simpleService";@Overridepublic void onCreate() {Log.d(tag, "simpleserviece ->onCreate()");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(tag, "simpleserviece ->onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.d(tag, "simpleserviece ->onDestroy()");super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {return null;}}
第三步配置文件在<application>节点下加入子节点(与activity节点平级):
<service android:name="com.example.simpleservice.SimpleService" >
</service>
第四步在MainActivity中加入按钮点击方法:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void dostartService(View v){Intent intent = new Intent(this, SimpleService.class);startService(intent);}public void dostopService(View v){Intent intent = new Intent(this, SimpleService.class);stopService(intent);}}
0 0
- Service 的生命周期
- service的生命周期
- Service的生命周期
- Service生命周期的学习
- Service 的生命周期
- Service 的生命周期
- Service的生命周期!!!
- Service的生命周期
- service的生命周期
- Android Service的生命周期
- Android 的Service生命周期
- Android Service 的生命周期
- service的生命周期
- Service的生命周期
- Service的生命周期
- Service的生命周期
- service的生命周期
- Android Service的生命周期
- ScrollView中添加listView
- COPY 淘宝图片服务的学习
- Html5添加SVG生成图案花纹背景样式的js插件教程
- 双边滤波(bilateral filter)灰度图 matlab实现代码
- Spring MVC和Struts2的比较
- Service的生命周期
- 常见编程语言的OEP入口点特征
- 使用Dapper接收多个结果集,并实现存储过程分页
- 新的。
- Python + Socket 通信
- outerHTML、innerHTML和innerText的区别
- OpenGL(5)--太阳系和的日月地实现
- dwz学习笔记
- JavaScript高级程序设计--读书心得