Service学习之一--------服务生命周期
来源:互联网 发布:mac 删除文件夹的命令 编辑:程序博客网 时间:2024/05/20 10:24
1、要使用服务,第一件事是自己创建一个类继承Seivice:
package com.example.studyservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service {private static final String TAG = "Service";@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();Log.i(TAG, "onCreate");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.i(TAG, "onDestroy");}}
package com.example.studyservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void StartService(View view) {Intent service = new Intent();service.setClass(this, MyService.class);startService(service);}public void StopService(View view) {stopService(new Intent(this, MyService.class));}}
当服务开启的时候走的是:
onCreate() -------> onStartCommand;
当停止服务的时候,在上面的基础上走onDestroy()
(3)记得在manifest注册:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.studyservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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="com.example.studyservice.MyService" /> </application></manifest>
0 0
- Service学习之一--------服务生命周期
- service服务的生命周期
- Android重要组件之一 Service 服务讲解学习(一)
- Android Service服务的生命周期
- Android Service 服务及生命周期
- Android:Service:服务的生命周期
- AND(错误) Service服务之一
- Service服务---四大组件之一
- Service生命周期的学习
- Android Service学习之 Service生命周期 & 绑定类型服务& Binder类
- Android Service---管理服务的生命周期
- Android Service---管理服务的生命周期
- Android管理服务(Service)的生命周期(lifecycle)
- Android Service---管理服务的生命周期
- 管理服务的生命周期 Android Service
- Android管理服务(Service)的生命周期(lifecycle)
- 服务service生命周期及方法使用
- Android Service学习笔记--Service生命周期
- MVP与MVC的异同
- 参数加起来的和转为¥格式
- 用spring MVC 生成Excel和PDF
- 关于Liunux编程的一些记录
- git(版本控制系统)的使用
- Service学习之一--------服务生命周期
- .NET框架学习: 强命名的作用
- HDFS--笔记
- 购物车结算
- 最小生成树之Kruskal算法
- 用户级线程和内核级线程的区别
- web_reg_save_param用法
- Python3-正则表达式-创建、匹配Regex对象
- 控件抖动