基础篇1.9 Service简介

2024-01-19 13:08
文章标签 基础 简介 service 1.9

本文主要是介绍基础篇1.9 Service简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基础篇1.9 Service简介

一、service是什么

1、  Service是应用程序组件,和Activity属于同一层次。

2、  Service没有图形化界面。

3、  Service通常用来处理一些耗时比较长的操作。如下载等。

4、  可以使用Service更新ContentProvider,发送Intent以及启动系统通知等。

二、Service生命周期

1、  启动Service:Context.startService()

2、  停止Service:Context.stopService()

三、应用举例:

定义MainActivity:

package com.solidwang.service_01;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public classMainActivity extends Activity {

   private Button startService;

   private Button stopService;

   @Override

   protected void onCreate(BundlesavedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      startService = (Button)findViewById(R.id.startService);

      stopService = (Button)findViewById(R.id.stopService);

      startService.setOnClickListener(newStartServiceListener());

      stopService.setOnClickListener(new StopServiceListener());

   }

   //启动Service

   class StartServiceListener implements OnClickListener{

      @Override

      public void onClick(View v) {

         Intentintent = newIntent();

         intent.setClass(MainActivity.this, MyService.class);

         startService(intent);

      }

   }

   //停止Service

   class StopServiceListener implements OnClickListener{

      @Override

      public void onClick(View v) {

         Intentintent = newIntent();

         intent.setClass(MainActivity.this, MyService.class);

         stopService(intent);

      }

   }

}

 

MyService中重写Service的方法:

packagecom.solidwang.service_01;

 

importandroid.app.Service;

importandroid.content.Intent;

importandroid.os.IBinder;

 

publicclass MyService extends Service {

 

         //初次启动Service的时候调用

         //如果Service已经处于启动状态,再次启动启动不会再执行onCreate

         @Override

         public void onCreate() {

                   System.out.println("---onCreate---");

         }

 

         //执行完毕onCreate后执行onStartCommand

         @Override

         public int onStartCommand(Intentintent, int flags, int startId) {

                   System.out.println("intent="+ intent + ", flags=" + flags + ", startId=" + startId);

                   return START_NOT_STICKY;

         }

        

         //停止Service时执行

         @Override

         public void onDestroy() {

                   System.out.println("---onDestroy---");

         }

 

         @Override

         public IBinder onBind(Intent arg0) {

                   // TODO Auto-generated methodstub

                   return null;

         }

}

 

在AndroidManifest.xml中注册:

<service android:name=".MyService" />

 

运行结果:


这篇关于基础篇1.9 Service简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/622524

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

Golang的CSP模型简介(最新推荐)

《Golang的CSP模型简介(最新推荐)》Golang采用了CSP(CommunicatingSequentialProcesses,通信顺序进程)并发模型,通过goroutine和channe... 目录前言一、介绍1. 什么是 CSP 模型2. Goroutine3. Channel4. Channe